Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,71 @@
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
"""
|
| 7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 8 |
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
max_tokens,
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
)
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
messages.append({"role": "user", "content": val[0]})
|
| 23 |
-
if val[1]:
|
| 24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
| 25 |
|
| 26 |
-
|
|
|
|
| 27 |
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
temperature=temperature,
|
| 35 |
-
top_p=top_p,
|
| 36 |
-
):
|
| 37 |
-
token = message.choices[0].delta.content
|
| 38 |
|
| 39 |
-
|
| 40 |
-
yield response
|
| 41 |
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 50 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 51 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 52 |
-
gr.Slider(
|
| 53 |
-
minimum=0.1,
|
| 54 |
-
maximum=1.0,
|
| 55 |
-
value=0.95,
|
| 56 |
-
step=0.05,
|
| 57 |
-
label="Top-p (nucleus sampling)",
|
| 58 |
-
),
|
| 59 |
],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
)
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
demo.launch()
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
import os
|
| 3 |
import gradio as gr
|
| 4 |
+
from langchain.document_loaders import CSVLoader
|
| 5 |
+
from langchain.vectorstores import FAISS
|
| 6 |
+
from langchain.embeddings import HuggingFaceEmbeddings
|
| 7 |
+
from langchain.chains import RetrievalQA
|
| 8 |
+
from langchain_groq import ChatGroq
|
| 9 |
|
| 10 |
+
# Set up your API key for ChatGroq
|
| 11 |
+
os.environ["GROQ_API_KEY"] = "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" # Replace with your actual API key
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
# Initialize the HuggingFace embeddings
|
| 14 |
+
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2") # Lightweight embedding model
|
| 15 |
|
| 16 |
+
# Instantiate the ChatGroq model
|
| 17 |
+
llm = ChatGroq(
|
| 18 |
+
model="mixtral-8x7b-32768", # Replace with your desired model
|
| 19 |
+
temperature=0,
|
| 20 |
+
max_tokens=None,
|
| 21 |
+
timeout=None,
|
| 22 |
+
max_retries=2
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
# Define the function to process the query and CSV
|
| 26 |
+
def process_query(file, query):
|
| 27 |
+
try:
|
| 28 |
+
# Load the CSV as documents for retrieval
|
| 29 |
+
loader = CSVLoader(file_path=file.name)
|
| 30 |
+
documents = loader.load()
|
| 31 |
|
| 32 |
+
# Create a FAISS vector store
|
| 33 |
+
vector_store = FAISS.from_documents(documents, embeddings)
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
+
# Create a retriever from the vector store
|
| 36 |
+
retriever = vector_store.as_retriever()
|
| 37 |
|
| 38 |
+
# Create a RetrievalQA pipeline
|
| 39 |
+
qa_chain = RetrievalQA.from_chain_type(
|
| 40 |
+
llm=llm,
|
| 41 |
+
retriever=retriever,
|
| 42 |
+
return_source_documents=True
|
| 43 |
+
)
|
| 44 |
|
| 45 |
+
# Get the response
|
| 46 |
+
response = qa_chain({"query": query})
|
| 47 |
+
result = response["result"]
|
| 48 |
+
sources = "\n".join([doc.page_content for doc in response["source_documents"]])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
+
return result, sources
|
|
|
|
| 51 |
|
| 52 |
+
except Exception as e:
|
| 53 |
+
return f"An error occurred: {str(e)}", ""
|
| 54 |
|
| 55 |
+
# Create a Gradio interface
|
| 56 |
+
interface = gr.Interface(
|
| 57 |
+
fn=process_query,
|
| 58 |
+
inputs=[
|
| 59 |
+
gr.File(label="Upload CSV File"), # File input for the CSV
|
| 60 |
+
gr.Textbox(label="Enter your query") # Text input for the query
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
],
|
| 62 |
+
outputs=[
|
| 63 |
+
gr.Textbox(label="Answer"), # Text output for the answer
|
| 64 |
+
gr.Textbox(label="Source Documents") # Text output for the source documents
|
| 65 |
+
],
|
| 66 |
+
title="CSV Query Assistant",
|
| 67 |
+
description="Upload a CSV file and enter a query to retrieve relevant information."
|
| 68 |
)
|
| 69 |
|
| 70 |
+
# Launch the Gradio app
|
| 71 |
+
interface.launch(share=True)
|
|
|