Spaces:
Runtime error
Runtime error
Delete app.py
Browse files
app.py
DELETED
|
@@ -1,68 +0,0 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
import os
|
| 3 |
-
from langchain_community.chat_models import ChatOpenAI
|
| 4 |
-
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 5 |
-
from langchain.vectorstores import FAISS
|
| 6 |
-
from langchain.embeddings import OpenAIEmbeddings
|
| 7 |
-
from langchain.chains import RetrievalQA
|
| 8 |
-
from PyPDF2 import PdfReader
|
| 9 |
-
import docx
|
| 10 |
-
|
| 11 |
-
# Configuració Streamlit
|
| 12 |
-
st.set_page_config(page_title="Agent IA Corporatiu", layout="wide")
|
| 13 |
-
st.markdown("<h1 style='color:#1986aa;'>Agent IA Corporatiu amb RAG</h1>", unsafe_allow_html=True)
|
| 14 |
-
|
| 15 |
-
# Comprovació de la clau OpenAI
|
| 16 |
-
OPENAI_API_KEY = st.secrets.get("OPENAI_API_KEY") or os.getenv("OPENAI_API_KEY")
|
| 17 |
-
if not OPENAI_API_KEY:
|
| 18 |
-
st.error("⚠️ Falta la clau OpenAI. Comprova Secrets i reinicia el Space.")
|
| 19 |
-
st.stop()
|
| 20 |
-
else:
|
| 21 |
-
st.write("✅ Clau carregada correctament.")
|
| 22 |
-
|
| 23 |
-
# Inicialitzar model
|
| 24 |
-
llm = ChatOpenAI(openai_api_key=OPENAI_API_KEY, model="gpt-3.5-turbo", temperature=0)
|
| 25 |
-
|
| 26 |
-
# Funcions per processar documents
|
| 27 |
-
def process_pdf(file):
|
| 28 |
-
reader = PdfReader(file)
|
| 29 |
-
text = ""
|
| 30 |
-
for page in reader.pages:
|
| 31 |
-
text += page.extract_text() + "\n"
|
| 32 |
-
return text
|
| 33 |
-
|
| 34 |
-
def process_docx(file):
|
| 35 |
-
doc = docx.Document(file)
|
| 36 |
-
return "\n".join([p.text for p in doc.paragraphs])
|
| 37 |
-
|
| 38 |
-
# Pujada de documents
|
| 39 |
-
uploaded_files = st.file_uploader("📂 Pujar documents (PDF o Word)", type=["pdf", "docx"], accept_multiple_files=True)
|
| 40 |
-
|
| 41 |
-
# Indexació
|
| 42 |
-
if uploaded_files:
|
| 43 |
-
st.info("Indexant documents...")
|
| 44 |
-
all_text = ""
|
| 45 |
-
for file in uploaded_files:
|
| 46 |
-
if file.type == "application/pdf":
|
| 47 |
-
all_text += process_pdf(file)
|
| 48 |
-
else:
|
| 49 |
-
all_text += process_docx(file)
|
| 50 |
-
|
| 51 |
-
splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=100)
|
| 52 |
-
docs = splitter.split_text(all_text)
|
| 53 |
-
|
| 54 |
-
embeddings = OpenAIEmbeddings(openai_api_key=OPENAI_API_KEY)
|
| 55 |
-
vectorstore = FAISS.from_texts(docs, embeddings)
|
| 56 |
-
retriever = vectorstore.as_retriever()
|
| 57 |
-
qa_chain = RetrievalQA.from_chain_type(llm, retriever=retriever)
|
| 58 |
-
|
| 59 |
-
st.success("✅ Documents indexats correctament!")
|
| 60 |
-
|
| 61 |
-
# Camp de consulta
|
| 62 |
-
query = st.text_input("🔍 Escriu la teva consulta:")
|
| 63 |
-
if query:
|
| 64 |
-
with st.spinner("Consultant IA..."):
|
| 65 |
-
result = qa_chain.run(query)
|
| 66 |
-
st.markdown(f"**Resposta:** {result}")
|
| 67 |
-
else:
|
| 68 |
-
st.warning("Puja documents per començar.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|