Syluh27 commited on
Commit ·
d76b078
1
Parent(s): d530882
- app.py +17 -0
- model.py +35 -0
- requirements.txt +5 -0
app.py
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from model import rag_chain
|
| 3 |
+
|
| 4 |
+
st.set_page_config(page_title="Chatbot Legal 🇪🇨", page_icon="⚖️")
|
| 5 |
+
|
| 6 |
+
st.title("🤖 Chatbot sobre el Código Penal de Ecuador ⚖️")
|
| 7 |
+
st.write("Este agente conversacional responde preguntas sobre las leyes del Código Penal ecuatoriano.")
|
| 8 |
+
|
| 9 |
+
user_input = st.text_input("✍️ Escribe tu pregunta:")
|
| 10 |
+
|
| 11 |
+
if st.button("🔎 Consultar"):
|
| 12 |
+
if user_input:
|
| 13 |
+
response = rag_chain.invoke({"query": user_input})
|
| 14 |
+
st.write("### 📜 Respuesta:")
|
| 15 |
+
st.write(response)
|
| 16 |
+
else:
|
| 17 |
+
st.warning("⚠️ Escribe una pregunta antes de consultar.")
|
model.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain.chains import RetrievalQA
|
| 2 |
+
from langchain.vectorstores import Chroma
|
| 3 |
+
from langchain.embeddings import HuggingFaceEmbeddings
|
| 4 |
+
from langchain.llms import ChatMistralAI
|
| 5 |
+
import chromadb
|
| 6 |
+
from huggingface_hub import hf_hub_download
|
| 7 |
+
import os
|
| 8 |
+
# Descargar los archivos de embeddings desde Hugging Face
|
| 9 |
+
embedding_path = hf_hub_download(repo_id="VictorCarr02/Conversational-Agent-LawsEC", filename="mis_embeddings")
|
| 10 |
+
chroma_path = hf_hub_download(repo_id="VictorCarr02/Conversational-Agent-LawsEC", filename="chroma/chroma.sqlite3")
|
| 11 |
+
|
| 12 |
+
# Cargar ChromaDB y los embeddings
|
| 13 |
+
chromadb_client = chromadb.PersistentClient(path=chroma_path)
|
| 14 |
+
collection = chromadb_client.get_or_create_collection(name="mis_embeddings")
|
| 15 |
+
embeddings = HuggingFaceEmbeddings(model_name="mistralai/MistralAIEmbeddings", path=embedding_path)
|
| 16 |
+
vector_store = Chroma(collection=collection, embedding_function=embeddings)
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
# Acceder a la clave API desde la variable de entorno
|
| 21 |
+
api_key = os.getenv("MISTRAL_API_KEY")
|
| 22 |
+
|
| 23 |
+
# Verifica si la clave fue obtenida correctamente
|
| 24 |
+
if api_key is None:
|
| 25 |
+
raise ValueError("La clave API MISTRAL_API_KEY no está configurada como variable de entorno.")
|
| 26 |
+
|
| 27 |
+
# Crear el modelo LLM con la clave API
|
| 28 |
+
llm = ChatMistralAI(api_key=api_key)
|
| 29 |
+
|
| 30 |
+
# Crear el agente RAG
|
| 31 |
+
rag_chain = RetrievalQA.from_chain_type(
|
| 32 |
+
llm=llm,
|
| 33 |
+
retriever=vector_store.as_retriever(),
|
| 34 |
+
chain_type="stuff"
|
| 35 |
+
)
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
langchain
|
| 3 |
+
chromadb
|
| 4 |
+
huggingface_hub
|
| 5 |
+
MistralAIEmbeddings
|