Download app.py from mkfallah/sign: direct link, hf CLI and curl.
- Browser
- Download file 1.13 kB
-
https://huggingface.co/spaces/mkfallah/sign/resolve/main/app.py
- Command line
-
hf download hf://spaces/mkfallah/sign/app.py
-
curl -L -o app.py https://huggingface.co/spaces/mkfallah/sign/resolve/main/app.py
1.13 kB
| import gradio as gr | |
| from sentence_transformers import SentenceTransformer, util | |
| import os | |
| # Load data | |
| with open("data/handshape_book.txt", encoding="utf-8") as f: | |
| book_text = f.read() | |
| # Split into chunks | |
| chunks = [chunk.strip() for chunk in book_text.split('\n\n') if len(chunk.strip()) > 100] | |
| # Create embeddings | |
| model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2") | |
| corpus_embeddings = model.encode(chunks, convert_to_tensor=True) | |
| # Search function | |
| def ask_question(question): | |
| question_embedding = model.encode(question, convert_to_tensor=True) | |
| hits = util.semantic_search(question_embedding, corpus_embeddings, top_k=3)[0] | |
| results = "\n\n---\n\n".join([chunks[hit['corpus_id']] for hit in hits]) | |
| return results | |
| # Gradio UI | |
| iface = gr.Interface(fn=ask_question, | |
| inputs=gr.Textbox(lines=2, placeholder="Ask about handshapes..."), | |
| outputs="text", | |
| title="ASL Handshape RAG", | |
| description="Semantic search over American Sign Language Handshape Dictionary.") | |
| iface.launch() | |