Spaces:
Sleeping
Sleeping
daniel.diaz
commited on
Commit
·
f07eaf3
1
Parent(s):
4d5cbf4
Cambios para API
Browse files
app.py
CHANGED
|
@@ -1,113 +1,91 @@
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
-
|
| 3 |
-
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 4 |
-
from sentence_transformers import SentenceTransformer
|
| 5 |
-
import openai
|
| 6 |
-
import faiss
|
| 7 |
import numpy as np
|
|
|
|
| 8 |
import os
|
| 9 |
-
import joblib
|
| 10 |
-
from openai import OpenAI
|
| 11 |
-
|
| 12 |
-
os.environ["TRANSFORMERS_CACHE"] = "/tmp"
|
| 13 |
-
os.environ["HF_HOME"] = "/tmp"
|
| 14 |
-
os.environ["XDG_CACHE_HOME"] = "/tmp"
|
| 15 |
-
os.environ["SENTENCE_TRANSFORMERS_HOME"] = "/tmp"
|
| 16 |
-
os.environ["STREAMLIT_HOME"] = "/tmp"
|
| 17 |
-
|
| 18 |
from openai import OpenAI
|
| 19 |
|
|
|
|
| 20 |
client = OpenAI(api_key=os.getenv("POCJujitsu"))
|
| 21 |
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
|
| 24 |
-
def
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
return splitter.split_text(raw_text)
|
| 31 |
-
|
| 32 |
-
@st.cache_resource
|
| 33 |
-
def load_model_and_index(chunks):
|
| 34 |
-
model = SentenceTransformer('models/all-MiniLM-L6-v2')
|
| 35 |
-
embeddings = model.encode(chunks)
|
| 36 |
-
faiss_index = faiss.IndexFlatL2(embeddings.shape[1])
|
| 37 |
-
faiss_index.add(np.array(embeddings))
|
| 38 |
-
# joblib.dump((model, chunks, faiss_index), "rag_model.joblib")
|
| 39 |
-
return model, chunks, faiss_index
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
| 45 |
|
| 46 |
-
|
|
|
|
| 47 |
response = client.chat.completions.create(
|
| 48 |
-
model="gpt-
|
| 49 |
-
messages=[
|
| 50 |
-
{"role": "user", "content": question}
|
| 51 |
-
],
|
| 52 |
temperature=0.5,
|
| 53 |
-
max_tokens=
|
| 54 |
)
|
| 55 |
return response.choices[0].message.content
|
| 56 |
|
| 57 |
-
def chat_with_rag(question,
|
| 58 |
-
context = "\n".join(
|
| 59 |
-
prompt =
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
response = client.chat.completions.create(
|
| 62 |
-
model="gpt-
|
| 63 |
messages=[{"role": "user", "content": prompt}],
|
| 64 |
temperature=0.3,
|
| 65 |
-
max_tokens=
|
| 66 |
)
|
| 67 |
return response.choices[0].message.content
|
| 68 |
|
| 69 |
-
def chat_with_rag_enhanced(question,
|
| 70 |
-
context = "\n".join(
|
| 71 |
prompt = (
|
| 72 |
"Eres un experto en historia marcial. "
|
| 73 |
-
"Usa el siguiente contexto
|
|
|
|
| 74 |
f"Contexto:\n{context}\n\n"
|
| 75 |
f"Pregunta: {question}\nRespuesta:"
|
| 76 |
)
|
| 77 |
-
|
| 78 |
response = client.chat.completions.create(
|
| 79 |
-
model="gpt-
|
| 80 |
messages=[{"role": "user", "content": prompt}],
|
| 81 |
temperature=0.2,
|
| 82 |
-
max_tokens=
|
| 83 |
)
|
| 84 |
return response.choices[0].message.content
|
| 85 |
|
| 86 |
# Streamlit UI
|
| 87 |
-
st.
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
st.
|
| 106 |
-
st.write(chat_no_rag(query, max_tokens=max_tokens))
|
| 107 |
-
|
| 108 |
-
st.subheader("🔹 Respuesta con RAG:")
|
| 109 |
-
retrieved = search(query, model, chunks, index)
|
| 110 |
-
st.write(chat_with_rag(query, retrieved, max_tokens=max_tokens))
|
| 111 |
-
|
| 112 |
-
st.subheader("🔹 Respuesta con RAG + Mejora de Prompt:")
|
| 113 |
-
st.write(chat_with_rag_enhanced(query, retrieved, max_tokens=max_tokens))
|
|
|
|
| 1 |
+
|
| 2 |
import streamlit as st
|
| 3 |
+
import joblib
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
import numpy as np
|
| 5 |
+
import faiss
|
| 6 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
from openai import OpenAI
|
| 8 |
|
| 9 |
+
# Initialize OpenAI client using custom environment variable set in Hugging Face
|
| 10 |
client = OpenAI(api_key=os.getenv("POCJujitsu"))
|
| 11 |
|
| 12 |
+
# Load serialized FAISS index and document chunks
|
| 13 |
+
chunks, index = joblib.load("rag_model.joblib")
|
| 14 |
|
| 15 |
+
# Embed query using OpenAI embedding API
|
| 16 |
+
def embed_query(text):
|
| 17 |
+
response = client.embeddings.create(
|
| 18 |
+
model="text-embedding-3-small",
|
| 19 |
+
input=text
|
| 20 |
+
)
|
| 21 |
+
return np.array(response.data[0].embedding, dtype=np.float32).reshape(1, -1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
# Semantic search using FAISS
|
| 24 |
+
def search(query, k=3):
|
| 25 |
+
query_vec = embed_query(query).astype(np.float32)
|
| 26 |
+
distances, labels = index.search(query_vec, k)
|
| 27 |
+
return [chunks[i] for i in labels[0]]
|
| 28 |
|
| 29 |
+
# Chat modes
|
| 30 |
+
def chat_no_rag(question):
|
| 31 |
response = client.chat.completions.create(
|
| 32 |
+
model="gpt-3.5-turbo",
|
| 33 |
+
messages=[{"role": "user", "content": question}],
|
|
|
|
|
|
|
| 34 |
temperature=0.5,
|
| 35 |
+
max_tokens=300
|
| 36 |
)
|
| 37 |
return response.choices[0].message.content
|
| 38 |
|
| 39 |
+
def chat_with_rag(question, context_chunks):
|
| 40 |
+
context = "\n".join(context_chunks)
|
| 41 |
+
prompt = (
|
| 42 |
+
"Usa el siguiente contexto como referencia para responder la pregunta. "
|
| 43 |
+
"Puedes complementar con tus propios conocimientos si es necesario.\n\n"
|
| 44 |
+
f"Contexto:\n{context}\n\n"
|
| 45 |
+
f"Pregunta: {question}\nRespuesta:"
|
| 46 |
+
)
|
| 47 |
response = client.chat.completions.create(
|
| 48 |
+
model="gpt-3.5-turbo",
|
| 49 |
messages=[{"role": "user", "content": prompt}],
|
| 50 |
temperature=0.3,
|
| 51 |
+
max_tokens=300
|
| 52 |
)
|
| 53 |
return response.choices[0].message.content
|
| 54 |
|
| 55 |
+
def chat_with_rag_enhanced(question, context_chunks):
|
| 56 |
+
context = "\n".join(context_chunks)
|
| 57 |
prompt = (
|
| 58 |
"Eres un experto en historia marcial. "
|
| 59 |
+
"Usa el siguiente contexto como referencia para responder la pregunta. "
|
| 60 |
+
"Puedes complementar con tus propios conocimientos si es necesario.\n\n"
|
| 61 |
f"Contexto:\n{context}\n\n"
|
| 62 |
f"Pregunta: {question}\nRespuesta:"
|
| 63 |
)
|
|
|
|
| 64 |
response = client.chat.completions.create(
|
| 65 |
+
model="gpt-3.5-turbo",
|
| 66 |
messages=[{"role": "user", "content": prompt}],
|
| 67 |
temperature=0.2,
|
| 68 |
+
max_tokens=300
|
| 69 |
)
|
| 70 |
return response.choices[0].message.content
|
| 71 |
|
| 72 |
# Streamlit UI
|
| 73 |
+
st.set_page_config(page_title="RAG JuJutsu Q&A")
|
| 74 |
+
st.title("🤖 JuJutsu AI - Ask Anything")
|
| 75 |
+
st.markdown("Ask a question about jujutsu history, techniques, or philosophy.")
|
| 76 |
+
|
| 77 |
+
question = st.text_input("❓ Enter your question:")
|
| 78 |
+
mode = st.radio("Choose response mode:", ["No RAG", "With RAG", "With RAG + Expert Prompt"])
|
| 79 |
+
|
| 80 |
+
if st.button("Get Answer") and question:
|
| 81 |
+
if mode == "No RAG":
|
| 82 |
+
answer = chat_no_rag(question)
|
| 83 |
+
else:
|
| 84 |
+
retrieved = search(question)
|
| 85 |
+
if mode == "With RAG":
|
| 86 |
+
answer = chat_with_rag(question, retrieved)
|
| 87 |
+
else:
|
| 88 |
+
answer = chat_with_rag_enhanced(question, retrieved)
|
| 89 |
+
|
| 90 |
+
st.markdown("### 🧠 Answer")
|
| 91 |
+
st.write(answer)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|