Sujatha commited on
Commit
53bf733
·
verified ·
1 Parent(s): e825b64

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -17
app.py CHANGED
@@ -130,13 +130,13 @@ You have access to:
130
  When users ask about stocks or indices, fetch live data. Provide clear explanations with numbers."""
131
 
132
  # ========== Core conversational function ==========
133
- def process_query(query: str, history: List[Dict], exchange: str) -> Tuple[str, List[Dict], pd.DataFrame, str]:
134
  """
135
  Process user query with real-time data awareness.
136
- history is a list of dicts: [{"role": "user", "content": "..."}, {"role": "assistant", "content": "..."}, ...]
137
  Returns:
138
  - response_text: str (the assistant's reply)
139
- - updated_history: list of dicts (new chat history)
140
  - data_preview: pd.DataFrame
141
  - status: str (unused)
142
  """
@@ -210,13 +210,11 @@ def process_query(query: str, history: List[Dict], exchange: str) -> Tuple[str,
210
  llm = get_llm()
211
  from langchain_core.messages import HumanMessage, AIMessage, SystemMessage
212
 
213
- # Convert Gradio history (list of dicts) to flat list for LangChain
214
  flat_history = []
215
- for turn in history:
216
- if turn["role"] == "user":
217
- flat_history.append(turn["content"])
218
- else: # assistant
219
- flat_history.append(turn["content"])
220
 
221
  messages = [SystemMessage(content=SYSTEM_PROMPT)]
222
  for i, msg in enumerate(flat_history):
@@ -231,14 +229,11 @@ def process_query(query: str, history: List[Dict], exchange: str) -> Tuple[str,
231
  except Exception as e:
232
  response_text = f"LLM error: {str(e)}"
233
 
234
- # Update history in Gradio's new format (list of dicts)
235
- updated_history = history + [
236
- {"role": "user", "content": query},
237
- {"role": "assistant", "content": response_text}
238
- ]
239
  # Keep only last 10 exchanges (20 messages)
240
- if len(updated_history) > 20:
241
- updated_history = updated_history[-20:]
242
 
243
  return response_text, updated_history, data_preview, ""
244
 
@@ -366,7 +361,8 @@ with gr.Blocks(title="AI-Powered Index Analyst with Real-Time Data") as demo:
366
  """)
367
 
368
  with gr.Tab("💬 Conversational Assistant"):
369
- chat_history = gr.Chatbot(label="Conversation", height=400, type="messages")
 
370
  msg = gr.Textbox(
371
  label="Ask about stocks or indices",
372
  placeholder="E.g., 'What's the price of RELIANCE?' or 'Compare Nifty 50 index using Laspeyres and Fisher'"
 
130
  When users ask about stocks or indices, fetch live data. Provide clear explanations with numbers."""
131
 
132
  # ========== Core conversational function ==========
133
+ def process_query(query: str, history: List[List], exchange: str) -> Tuple[str, List[List], pd.DataFrame, str]:
134
  """
135
  Process user query with real-time data awareness.
136
+ history is a list of lists: [[user_msg1, assistant_msg1], [user_msg2, assistant_msg2], ...]
137
  Returns:
138
  - response_text: str (the assistant's reply)
139
+ - updated_history: list of lists (new chat history)
140
  - data_preview: pd.DataFrame
141
  - status: str (unused)
142
  """
 
210
  llm = get_llm()
211
  from langchain_core.messages import HumanMessage, AIMessage, SystemMessage
212
 
213
+ # Convert Gradio history (list of lists) to flat list for LangChain
214
  flat_history = []
215
+ for user_msg, assistant_msg in history:
216
+ flat_history.append(user_msg)
217
+ flat_history.append(assistant_msg)
 
 
218
 
219
  messages = [SystemMessage(content=SYSTEM_PROMPT)]
220
  for i, msg in enumerate(flat_history):
 
229
  except Exception as e:
230
  response_text = f"LLM error: {str(e)}"
231
 
232
+ # Update history in Gradio's classic format (list of lists)
233
+ updated_history = history + [[query, response_text]]
 
 
 
234
  # Keep only last 10 exchanges (20 messages)
235
+ if len(updated_history) > 10:
236
+ updated_history = updated_history[-10:]
237
 
238
  return response_text, updated_history, data_preview, ""
239
 
 
361
  """)
362
 
363
  with gr.Tab("💬 Conversational Assistant"):
364
+ # Remove type="messages" – use classic list-of-lists format
365
+ chat_history = gr.Chatbot(label="Conversation", height=400)
366
  msg = gr.Textbox(
367
  label="Ask about stocks or indices",
368
  placeholder="E.g., 'What's the price of RELIANCE?' or 'Compare Nifty 50 index using Laspeyres and Fisher'"