Spaces:
Running
Running
Commit ·
cd839b3
0
Parent(s):
initial commit
Browse files- .DS_Store +0 -0
- README.md +10 -0
- __pycache__/rag_engine.cpython-313.pyc +0 -0
- app.py +36 -0
- documents.json +7 -0
- rag_engine.py +76 -0
- requirements.txt +8 -0
- tests/.DS_Store +0 -0
- tests/test_api.py +0 -0
.DS_Store
ADDED
|
Binary file (8.2 kB). View file
|
|
|
README.md
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: RAG QA System
|
| 3 |
+
emoji: 🤖
|
| 4 |
+
colorFrom: indigo
|
| 5 |
+
colorTo: blue
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: "6.9.0"
|
| 8 |
+
app_file: app.py
|
| 9 |
+
pinned: false
|
| 10 |
+
---
|
__pycache__/rag_engine.cpython-313.pyc
ADDED
|
Binary file (3.75 kB). View file
|
|
|
app.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import rag_engine
|
| 3 |
+
|
| 4 |
+
def ask(query, top_k, umbral):
|
| 5 |
+
docs = rag_engine.recuperar_documentos(query, top_k=top_k, umbral=umbral)
|
| 6 |
+
respuesta = rag_engine.generar_respuesta(query, docs)
|
| 7 |
+
|
| 8 |
+
docs_formateados = "\n\n---\n\n".join(docs)
|
| 9 |
+
|
| 10 |
+
return respuesta, docs_formateados
|
| 11 |
+
|
| 12 |
+
with gr.Blocks() as demo:
|
| 13 |
+
gr.Markdown("# Sistema de preguntas con RAG")
|
| 14 |
+
gr.Markdown("Haz una pregunta sobre la base de conocimiento.")
|
| 15 |
+
|
| 16 |
+
query = gr.Textbox(
|
| 17 |
+
label="Tu pregunta (en inglés)",
|
| 18 |
+
placeholder="Where is the hospital?"
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
top_k = gr.Slider(1, 5, value=2, step=1, label="Top-k documentos")
|
| 22 |
+
umbral = gr.Slider(0.0, 1.0, value=0.5, step=0.05, label="Umbral de similitud")
|
| 23 |
+
|
| 24 |
+
respuesta = gr.Textbox(label="Respuesta", lines=3)
|
| 25 |
+
docs = gr.Textbox(label="Documentos recuperados", lines=8)
|
| 26 |
+
|
| 27 |
+
boton = gr.Button("Enviar")
|
| 28 |
+
|
| 29 |
+
boton.click(
|
| 30 |
+
ask,
|
| 31 |
+
inputs=[query, top_k, umbral],
|
| 32 |
+
outputs=[respuesta, docs],
|
| 33 |
+
api_name="ask"
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
demo.launch()
|
documents.json
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"doc1": "Hospital contact details: You can contact the hospital at email testing@gmail.com, phone +911234567890, or visit us at xyz, abc, 1234, Nepal.",
|
| 3 |
+
"doc2": "Hospital's working hours: The hospital's working hours are 7:00 AM - 8:00 PM daily.",
|
| 4 |
+
"doc3": "Official email address: The official email address to contact the hospital is testing@gmail.com.",
|
| 5 |
+
"doc4": "Main services: We provide comprehensive healthcare services including emergency care, diagnostic testing, surgical procedures, maternity services, and specialized treatments.",
|
| 6 |
+
"doc5": "Hospital location: The hospital is located at xyz, abc, 1234, Nepal."
|
| 7 |
+
}
|
rag_engine.py
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import torch
|
| 3 |
+
from sentence_transformers import SentenceTransformer
|
| 4 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
| 5 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 6 |
+
|
| 7 |
+
with open("documents.json", "r", encoding="utf-8") as f:
|
| 8 |
+
documents = json.load(f)
|
| 9 |
+
|
| 10 |
+
docs_list = list(documents.values())
|
| 11 |
+
|
| 12 |
+
embedding_model = SentenceTransformer("MongoDB/mdbr-leaf-ir")
|
| 13 |
+
doc_embeddings = embedding_model.encode(docs_list)
|
| 14 |
+
|
| 15 |
+
tokenizer = AutoTokenizer.from_pretrained("PleIAs/Pleias-RAG-350M")
|
| 16 |
+
language_model = AutoModelForCausalLM.from_pretrained("PleIAs/Pleias-RAG-350M")
|
| 17 |
+
|
| 18 |
+
def recuperar_documentos(consulta, top_k=2, umbral=0.4):
|
| 19 |
+
consulta_embedding = embedding_model.encode([consulta])
|
| 20 |
+
similitudes = cosine_similarity(consulta_embedding, doc_embeddings)[0]
|
| 21 |
+
|
| 22 |
+
resultados = []
|
| 23 |
+
|
| 24 |
+
for i, score in enumerate(similitudes):
|
| 25 |
+
if score >= umbral:
|
| 26 |
+
resultados.append((score, docs_list[i]))
|
| 27 |
+
|
| 28 |
+
resultados.sort(key=lambda x: x[0], reverse=True)
|
| 29 |
+
|
| 30 |
+
documentos_recuperados = [doc for _, doc in resultados[:top_k]]
|
| 31 |
+
return documentos_recuperados
|
| 32 |
+
|
| 33 |
+
def generar_respuesta(consulta, documentos_recuperados):
|
| 34 |
+
contexto = " ".join(documentos_recuperados)
|
| 35 |
+
|
| 36 |
+
prompt = f"""Answer the question based only on the context provided
|
| 37 |
+
Context: {contexto}
|
| 38 |
+
Question: {consulta}
|
| 39 |
+
Answer:"""
|
| 40 |
+
|
| 41 |
+
inputs = tokenizer(prompt, return_tensors="pt", truncation=True)
|
| 42 |
+
|
| 43 |
+
with torch.no_grad():
|
| 44 |
+
output = language_model.generate(
|
| 45 |
+
**inputs,
|
| 46 |
+
max_new_tokens=60,
|
| 47 |
+
do_sample=False,
|
| 48 |
+
repetition_penalty=1.2,
|
| 49 |
+
pad_token_id=tokenizer.eos_token_id
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
respuesta_completa = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 53 |
+
|
| 54 |
+
if "Answer:" in respuesta_completa:
|
| 55 |
+
respuesta = respuesta_completa.split("Answer:")[-1].strip()
|
| 56 |
+
else:
|
| 57 |
+
respuesta = respuesta_completa.strip()
|
| 58 |
+
|
| 59 |
+
return respuesta
|
| 60 |
+
|
| 61 |
+
def preguntar(consulta, top_k=2, umbral=0.4):
|
| 62 |
+
documentos_recuperados = recuperar_documentos(consulta, top_k=top_k, umbral=umbral)
|
| 63 |
+
respuesta = generar_respuesta(consulta, documentos_recuperados)
|
| 64 |
+
return respuesta
|
| 65 |
+
|
| 66 |
+
if __name__ == "__main__":
|
| 67 |
+
pregunta = "Where is the hospital?"
|
| 68 |
+
docs = recuperar_documentos(pregunta)
|
| 69 |
+
respuesta = generar_respuesta(pregunta, docs)
|
| 70 |
+
|
| 71 |
+
print("Documentos encontrados:")
|
| 72 |
+
for i, doc in enumerate(docs, start=1):
|
| 73 |
+
print(f"{i}. {doc}")
|
| 74 |
+
|
| 75 |
+
print("\nRespuesta generada:")
|
| 76 |
+
print(respuesta)
|
requirements.txt
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch
|
| 2 |
+
transformers
|
| 3 |
+
sentence-transformers
|
| 4 |
+
scikit-learn
|
| 5 |
+
gradio
|
| 6 |
+
fastapi
|
| 7 |
+
uvicorn
|
| 8 |
+
pydantic
|
tests/.DS_Store
ADDED
|
Binary file (6.15 kB). View file
|
|
|
tests/test_api.py
ADDED
|
File without changes
|