Spaces:
Runtime error
Runtime error
Download app.py from FFernandes4283/Techlab: direct link, hf CLI and curl.
- Browser
- Download file 1.61 kB
-
https://huggingface.co/spaces/FFernandes4283/Techlab/resolve/main/app.py
- Command line
-
hf download hf://spaces/FFernandes4283/Techlab/app.py
-
curl -L -o app.py https://huggingface.co/spaces/FFernandes4283/Techlab/resolve/main/app.py
1.61 kB
| import gradio as gr | |
| import os | |
| from langchain_community.document_loaders import CSVLoader | |
| from langchain_community.vectorstores import FAISS | |
| from langchain_community.embeddings import HuggingFaceEmbeddings | |
| from langchain_core.prompts import ChatPromptTemplate | |
| from langchain_core.runnables import RunnablePassthrough | |
| from langchain import hub | |
| from langchain_community.llms import HuggingFaceHub | |
| loader = CSVLoader(file_path="data/test.csv") | |
| documents = loader.load() | |
| embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2") | |
| # --- Criar a base vetorial --- | |
| vectorstore = FAISS.from_documents(documents, embeddings) | |
| retriever = vectorstore.as_retriever() | |
| # --- Usar modelo gerativo gratuito (FLAN-T5) via HuggingFace Hub --- | |
| llm = HuggingFaceHub(repo_id="google/flan-t5-base", model_kwargs={"temperature": 0.2, "max_length": 512}) | |
| # --- Prompt RAG --- | |
| rag_template = """ | |
| VocΓͺ Γ© um assistente ΓΊtil que responde com base nos dados do arquivo CSV. | |
| Contexto: {context} | |
| Pergunta: {question} | |
| """ | |
| prompt = ChatPromptTemplate.from_template(rag_template) | |
| chain = ( | |
| {"context": retriever, "question": RunnablePassthrough()} | |
| | prompt | |
| | llm | |
| ) | |
| def responder(pergunta): | |
| resposta = chain.invoke(pergunta) | |
| return resposta | |
| iface = gr.Interface(fn=responder, | |
| inputs=gr.Textbox(lines=2, placeholder="Digite sua pergunta sobre o CSV..."), | |
| outputs="text", | |
| title="Chat com CSV (sem OpenAI)", | |
| description="Use um modelo gratuito para fazer perguntas sobre dados de um CSV.") | |
| iface.launch() | |