Spaces:
Paused
Paused
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import AutoTokenizer
|
| 3 |
+
from llm_engine import HuggingFaceEndpoint, ChatHuggingFace
|
| 4 |
+
from huggingface_hub import login
|
| 5 |
+
from PyPDF2 import PdfReader
|
| 6 |
+
from docx import Document
|
| 7 |
+
import csv
|
| 8 |
+
import json
|
| 9 |
+
import os
|
| 10 |
+
|
| 11 |
+
huggingface_token = os.getenv('HUGGINGFACE_TOKEN')
|
| 12 |
+
|
| 13 |
+
# Realizar el inicio de sesión de Hugging Face solo si el token está disponible
|
| 14 |
+
if huggingface_token:
|
| 15 |
+
login(token=huggingface_token)
|
| 16 |
+
|
| 17 |
+
# Configuración del modelo
|
| 18 |
+
@st.cache_resource
|
| 19 |
+
def load_llm():
|
| 20 |
+
llm = HuggingFaceEndpoint(
|
| 21 |
+
repo_id="mistralai/Mistral-7B-Instruct-v0.3",
|
| 22 |
+
task="text-generation"
|
| 23 |
+
)
|
| 24 |
+
llm_engine_hf = ChatHuggingFace(llm=llm)
|
| 25 |
+
tokenizer = AutoTokenizer.from_pretrained("mistralai/Mistral-7B-Instruct-v0.3")
|
| 26 |
+
return llm_engine_hf, tokenizer
|
| 27 |
+
|
| 28 |
+
llm_engine_hf, tokenizer = load_llm()
|
| 29 |
+
|
| 30 |
+
st.title("LexAIcon")
|
| 31 |
+
st.write("Puedes conversar con este chatbot basado en Mistral7B-Instruct y subir archivos para que el chatbot los procese.")
|
| 32 |
+
|
| 33 |
+
if "generated" not in st.session_state:
|
| 34 |
+
st.session_state["generated"] = []
|
| 35 |
+
if "past" not in st.session_state:
|
| 36 |
+
st.session_state["past"] = []
|
| 37 |
+
|
| 38 |
+
def generate_response(prompt):
|
| 39 |
+
response = llm_engine_hf.invoke(prompt)
|
| 40 |
+
return response
|
| 41 |
+
|
| 42 |
+
def handle_uploaded_file(uploaded_file):
|
| 43 |
+
try:
|
| 44 |
+
if uploaded_file.name.endswith(".txt"):
|
| 45 |
+
text = uploaded_file.read().decode("utf-8")
|
| 46 |
+
elif uploaded_file.name.endswith(".pdf"):
|
| 47 |
+
reader = PdfReader(uploaded_file)
|
| 48 |
+
text = ""
|
| 49 |
+
for page in range(len(reader.pages)):
|
| 50 |
+
text += reader.pages[page].extract_text()
|
| 51 |
+
elif uploaded_file.name.endswith(".docx"):
|
| 52 |
+
doc = Document(uploaded_file)
|
| 53 |
+
text = "\n".join([para.text for para in doc.paragraphs])
|
| 54 |
+
elif uploaded_file.name.endswith(".csv"):
|
| 55 |
+
text = ""
|
| 56 |
+
content = uploaded_file.read().decode("utf-8").splitlines()
|
| 57 |
+
reader = csv.reader(content)
|
| 58 |
+
text = " ".join([" ".join(row) for row in reader])
|
| 59 |
+
elif uploaded_file.name.endswith(".json"):
|
| 60 |
+
data = json.load(uploaded_file)
|
| 61 |
+
text = json.dumps(data, indent=4)
|
| 62 |
+
else:
|
| 63 |
+
text = "Tipo de archivo no soportado."
|
| 64 |
+
return text
|
| 65 |
+
except Exception as e:
|
| 66 |
+
return str(e)
|
| 67 |
+
|
| 68 |
+
# Entrada del usuario
|
| 69 |
+
user_input = st.text_input("Tú: ", "")
|
| 70 |
+
|
| 71 |
+
# Manejo de archivos subidos
|
| 72 |
+
uploaded_files = st.file_uploader("Sube un archivo", type=["txt", "pdf", "docx", "csv", "json"], accept_multiple_files=True)
|
| 73 |
+
|
| 74 |
+
if st.button("Enviar"):
|
| 75 |
+
if user_input:
|
| 76 |
+
response = generate_response(user_input)
|
| 77 |
+
st.session_state.generated.append({"user": user_input, "bot": response})
|
| 78 |
+
|
| 79 |
+
if st.session_state["generated"]:
|
| 80 |
+
for chat in st.session_state["generated"]:
|
| 81 |
+
st.write(f"Tú: {chat['user']}")
|
| 82 |
+
st.write(f"Chatbot: {chat['bot']}")
|
| 83 |
+
|
| 84 |
+
if uploaded_files:
|
| 85 |
+
for uploaded_file in uploaded_files:
|
| 86 |
+
st.write(f"Archivo subido: {uploaded_file.name}")
|
| 87 |
+
file_content = handle_uploaded_file(uploaded_file)
|
| 88 |
+
st.write(file_content)
|