Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +111 -0
- requirements.txt +8 -0
app.py
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
import io
|
| 4 |
+
import base64
|
| 5 |
+
import matplotlib.pyplot as plt
|
| 6 |
+
from langchain_openai import ChatOpenAI
|
| 7 |
+
from langchain_experimental.agents.agent_toolkits import create_csv_agent
|
| 8 |
+
from langchain.agents.agent_types import AgentType
|
| 9 |
+
from dotenv import load_dotenv
|
| 10 |
+
|
| 11 |
+
load_dotenv()
|
| 12 |
+
|
| 13 |
+
class CSVChatBot:
|
| 14 |
+
def __init__(self):
|
| 15 |
+
self.agent = None
|
| 16 |
+
self.file_path = None
|
| 17 |
+
|
| 18 |
+
def initialize_agent(self, file, api_key):
|
| 19 |
+
if file is None or api_key.strip() == "":
|
| 20 |
+
return "⚠ Please Enter your API key and Upload the CSV Data/File"
|
| 21 |
+
|
| 22 |
+
self.file_path = file.name
|
| 23 |
+
try:
|
| 24 |
+
model = ChatOpenAI(temperature=0.5, model="mistralai/mistral-7b-instruct:free",base_url="https://openrouter.ai/api/v1",api_key=os.getenv("OPENAI_API_KEY"))
|
| 25 |
+
self.agent = create_csv_agent(
|
| 26 |
+
llm=model,
|
| 27 |
+
path=self.file_path,
|
| 28 |
+
agent_type=AgentType.OPENAI_FUNCTIONS,
|
| 29 |
+
allow_dangerous_code=True,
|
| 30 |
+
agent_executor_kwargs=dict(handle_parsing_errors=True)
|
| 31 |
+
)
|
| 32 |
+
return "🟢 Congratulations!! CSV FIle Uploded and Agent is Initialized Successfully"
|
| 33 |
+
except Exception as e:
|
| 34 |
+
return f"Oops!! Error Occured because of: {e}"
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def chat(self, message, history):
|
| 38 |
+
if self.agent is None:
|
| 39 |
+
return "Please initialize the agent first by uploading a CSV file and providing an API key."
|
| 40 |
+
|
| 41 |
+
try:
|
| 42 |
+
CSV_PROMPT_PREFIX = "First get the column names from the CSV file, then answer the question."
|
| 43 |
+
CSV_PROMPT_SUFFIX = """
|
| 44 |
+
- **ALWAYS** before giving the Final Answer, try another method.
|
| 45 |
+
Then reflect on the answers of the two methods you did and ask yourself
|
| 46 |
+
if it answers correctly the original question.
|
| 47 |
+
If you are not sure, try another method.
|
| 48 |
+
- If the methods tried do not give the same result, reflect and
|
| 49 |
+
try again until you have two methods that have the same result.
|
| 50 |
+
- If you still cannot arrive to a consistent result, say that
|
| 51 |
+
you are not sure of the answer.
|
| 52 |
+
- If you are sure of the correct answer, create a beautiful
|
| 53 |
+
and thorough response using Markdown.
|
| 54 |
+
- **DO NOT MAKE UP AN ANSWER OR USE PRIOR KNOWLEDGE,
|
| 55 |
+
ONLY USE THE RESULTS OF THE CALCULATIONS YOU HAVE DONE**.
|
| 56 |
+
- **ALWAYS**, as part of your "Final Answer", explain how you got
|
| 57 |
+
to the answer on a section that starts with: "\n\nExplanation:\n".
|
| 58 |
+
In the explanation, mention the column names that you used to get
|
| 59 |
+
to the final answer.
|
| 60 |
+
"""
|
| 61 |
+
result = self.agent.run(CSV_PROMPT_PREFIX + message + CSV_PROMPT_SUFFIX)
|
| 62 |
+
fig = plt.gcf()
|
| 63 |
+
if fig.get_axes():
|
| 64 |
+
buf = io.BytesIO()
|
| 65 |
+
fig.savefig(buf, format='png')
|
| 66 |
+
buf.seek(0)
|
| 67 |
+
img_str = base64.b64encode(buf.getvalue()).decode()
|
| 68 |
+
img_markdown = f""
|
| 69 |
+
plt.clf()
|
| 70 |
+
plt.close(fig)
|
| 71 |
+
return result + "\n\n" + img_markdown
|
| 72 |
+
else:
|
| 73 |
+
return result
|
| 74 |
+
except Exception as e:
|
| 75 |
+
return f"An error occurred: {str(e)}"
|
| 76 |
+
|
| 77 |
+
csv_chatbot = CSVChatBot()
|
| 78 |
+
|
| 79 |
+
with gr.Blocks() as demo:
|
| 80 |
+
gr.Markdown("# CSV Analysis Chatbot")
|
| 81 |
+
|
| 82 |
+
with gr.Row():
|
| 83 |
+
file_input = gr.File(label="Upload CSV File/Data")
|
| 84 |
+
api_key_input = gr.Textbox(label="Enter OpenRouter API Key", type="password")
|
| 85 |
+
|
| 86 |
+
initialize_button = gr.Button("Initialize Agent")
|
| 87 |
+
init_output = gr.Textbox(label="Initialization Status")
|
| 88 |
+
|
| 89 |
+
chatbot = gr.Chatbot()
|
| 90 |
+
msg = gr.Textbox()
|
| 91 |
+
clear = gr.Button("Clear")
|
| 92 |
+
|
| 93 |
+
def user(user_message, history):
|
| 94 |
+
return "", history + [[user_message, None]]
|
| 95 |
+
|
| 96 |
+
def bot(history):
|
| 97 |
+
user_message = history[-1][0]
|
| 98 |
+
bot_message = csv_chatbot.chat(user_message, history)
|
| 99 |
+
history[-1][1] = bot_message
|
| 100 |
+
return history
|
| 101 |
+
|
| 102 |
+
def clear_chat():
|
| 103 |
+
return None
|
| 104 |
+
|
| 105 |
+
initialize_button.click(csv_chatbot.initialize_agent, inputs=[file_input, api_key_input], outputs=init_output)
|
| 106 |
+
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
|
| 107 |
+
bot, chatbot, chatbot
|
| 108 |
+
)
|
| 109 |
+
clear.click(clear_chat, None, chatbot, queue=False)
|
| 110 |
+
|
| 111 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
tabulate
|
| 3 |
+
langchain
|
| 4 |
+
matplotlib
|
| 5 |
+
langchain-openai
|
| 6 |
+
langchain-community
|
| 7 |
+
langchain-core
|
| 8 |
+
langchain-experimental
|