Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from langchain.embeddings.openai import OpenAIEmbeddings
|
| 3 |
+
from langchain.chat_models import ChatOpenAI
|
| 4 |
+
from langchain.chains import ConversationalRetrievalChain
|
| 5 |
+
from langchain.vectorstores import FAISS
|
| 6 |
+
from langchain.document_loaders.csv_loader import CSVLoader
|
| 7 |
+
|
| 8 |
+
def conversational_chat(query, lang_model_key, file_upload):
|
| 9 |
+
global chain, session_history
|
| 10 |
+
if file_upload is not None:
|
| 11 |
+
# Loading the CSV file
|
| 12 |
+
loader = CSVLoader(file_path=file_upload, encoding="utf-8")
|
| 13 |
+
data = loader.load()
|
| 14 |
+
|
| 15 |
+
# Initializing embeddings and vectors
|
| 16 |
+
embeddings = OpenAIEmbeddings(openai_api_key=lang_model_key)
|
| 17 |
+
vectors = FAISS.from_documents(data, embeddings)
|
| 18 |
+
|
| 19 |
+
# Creating the ConversationalRetrievalChain
|
| 20 |
+
chain = ConversationalRetrievalChain.from_llm(llm=ChatOpenAI(temperature=0.0, model_name='gpt-4', openai_api_key=lang_model_key),
|
| 21 |
+
retriever=vectors.as_retriever())
|
| 22 |
+
session_history = []
|
| 23 |
+
|
| 24 |
+
result = chain({"question": query, "chat_history": session_history})
|
| 25 |
+
session_history.append((query, result["answer"]))
|
| 26 |
+
return result["answer"]
|
| 27 |
+
|
| 28 |
+
iface = gr.Interface(fn=conversational_chat,
|
| 29 |
+
inputs=[gr.Textbox(label="Query", lines=7),
|
| 30 |
+
gr.Textbox(label="Your OpenAI API key:", type="password"),
|
| 31 |
+
gr.File(label="Upload your CSV file:", type="binary")],
|
| 32 |
+
outputs="text",
|
| 33 |
+
title="Conversational CSV Chat: Please upload your file and set your API Key in order to use the functionalities",
|
| 34 |
+
|
| 35 |
+
)
|
| 36 |
+
iface.launch()
|