|
|
import gradio as gr
|
|
|
from sentence_transformers import SentenceTransformer, util
|
|
|
import os
|
|
|
|
|
|
|
|
|
with open("data/handshape_book.txt", encoding="utf-8") as f:
|
|
|
book_text = f.read()
|
|
|
|
|
|
|
|
|
chunks = [chunk.strip() for chunk in book_text.split('\n\n') if len(chunk.strip()) > 100]
|
|
|
|
|
|
|
|
|
model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
|
|
|
corpus_embeddings = model.encode(chunks, convert_to_tensor=True)
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
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()
|
|
|
|