gael1130 commited on
Commit
75c7731
·
verified ·
1 Parent(s): 1f0e494

Update app.py

Browse files

Update with new UI to show history of questions

Files changed (1) hide show
  1. app.py +70 -22
app.py CHANGED
@@ -1,6 +1,5 @@
1
- import gradio as gr
2
- from huggingface_hub import InferenceClient
3
  import os
 
4
  import pandas as pd
5
  from langchain_together import ChatTogether
6
  from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
@@ -11,7 +10,10 @@ from langchain_core.messages import ToolMessage
11
  from langchain_core.runnables import RunnablePassthrough
12
  from operator import itemgetter
13
 
 
 
14
 
 
15
  def load_model(api_key):
16
  return ChatTogether(
17
  api_key=TOGETHER_API_KEY,
@@ -53,34 +55,80 @@ def create_chain(df, llm):
53
 
54
  return chain
55
 
 
 
 
 
 
 
 
 
56
  def process_query(csv_file, api_key, query):
57
  if not api_key.strip():
58
- return "Please provide an API key"
59
 
60
  try:
61
  df = pd.read_csv(csv_file.name)
62
  llm = load_model(api_key)
63
  chain = create_chain(df, llm)
64
  result = chain.invoke({"question": query})
65
- return f"Analysis Result:\n{result['response']}\n\nTechnical Details:\n{result['tool_output']}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
  except Exception as e:
67
- return f"Error: {str(e)}"
68
 
69
  # Create Gradio interface
70
- iface = gr.Interface(
71
- fn=process_query,
72
- inputs=[
73
- gr.File(label="Upload CSV File"),
74
- gr.Textbox(label="Together.ai API Key", type="password"),
75
- gr.Textbox(label="Your Question")
76
- ],
77
- outputs=gr.Textbox(label="Result"),
78
- title="CSV Analysis Assistant",
79
- description="Upload a CSV file and ask questions about it using natural language.",
80
- examples=[
81
- ["titanic_dataset.csv", "your-api-key-here", "Which columns have missing values?"],
82
- ["titanic_dataset.csv", "your-api-key-here", "What's the correlation between age and fare?"]
83
- ]
84
- )
85
-
86
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import os
2
+ import gradio as gr
3
  import pandas as pd
4
  from langchain_together import ChatTogether
5
  from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
 
10
  from langchain_core.runnables import RunnablePassthrough
11
  from operator import itemgetter
12
 
13
+ # Global variable to store QA history
14
+ qa_history = []
15
 
16
+
17
  def load_model(api_key):
18
  return ChatTogether(
19
  api_key=TOGETHER_API_KEY,
 
55
 
56
  return chain
57
 
58
+
59
+ def update_qa_history():
60
+ # Convert QA history to DataFrame for display
61
+ if not qa_history:
62
+ return pd.DataFrame(columns=["CSV File", "Question", "Answer"]).to_markdown()
63
+ return pd.DataFrame(qa_history, columns=["CSV File", "Question", "Answer"]).to_markdown()
64
+
65
+
66
  def process_query(csv_file, api_key, query):
67
  if not api_key.strip():
68
+ return "Please provide an API key", update_qa_history()
69
 
70
  try:
71
  df = pd.read_csv(csv_file.name)
72
  llm = load_model(api_key)
73
  chain = create_chain(df, llm)
74
  result = chain.invoke({"question": query})
75
+
76
+ # Format the response
77
+ response = f"Analysis Result:\n{result['response']}\n\nTechnical Details:\n{result['tool_output']}"
78
+
79
+ # Extract just the filename without path
80
+ filename = os.path.basename(csv_file.name)
81
+
82
+ # Add to QA history
83
+ qa_history.append([
84
+ filename, # Store only the filename
85
+ query,
86
+ result['response'] # Store just the human-readable response
87
+ ])
88
+
89
+ return response, update_qa_history()
90
  except Exception as e:
91
+ return f"Error: {str(e)}", update_qa_history()
92
 
93
  # Create Gradio interface
94
+ with gr.Blocks(title="CSV Analysis Assistant") as iface:
95
+ gr.Markdown("# CSV Analysis Assistant")
96
+ gr.Markdown("Upload a CSV file and ask questions about it using natural language.")
97
+
98
+ # Top section: Split into left (inputs) and right (result)
99
+ with gr.Row():
100
+ # Left column for inputs
101
+ with gr.Column(scale=1):
102
+ file_input = gr.File(label="Upload CSV File")
103
+ api_key = gr.Textbox(label="Together.ai API Key", type="password")
104
+ query = gr.Textbox(label="Your Question")
105
+ with gr.Row():
106
+ clear_btn = gr.Button("Clear")
107
+ submit_btn = gr.Button("Submit", variant="primary")
108
+
109
+ # Right column for result
110
+ with gr.Column(scale=1):
111
+ output = gr.Textbox(label="Result", lines=10)
112
+
113
+ # Bottom section: Full width for history table
114
+ with gr.Row():
115
+ history = gr.Markdown(value="### Question & Answer History\n" + update_qa_history())
116
+
117
+ # Handle button events
118
+ submit_btn.click(
119
+ fn=process_query,
120
+ inputs=[file_input, api_key, query],
121
+ outputs=[output, history]
122
+ )
123
+
124
+ def clear_inputs():
125
+ return [None, "", "", "", "### Question & Answer History\n" + update_qa_history()]
126
+
127
+ clear_btn.click(
128
+ fn=clear_inputs,
129
+ inputs=[],
130
+ outputs=[file_input, api_key, query, output, history]
131
+ )
132
+
133
+ # For Hugging Face Spaces deployment
134
+ iface.launch()