Cheh Kit Hong commited on
Commit
6b84d68
·
1 Parent(s): aa018e3

fixed gr message

Browse files
Files changed (2) hide show
  1. quick_check.py +16 -0
  2. ui/gradio_components.py +22 -11
quick_check.py ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+ print("Gradio Version:", gr.__version__)
4
+
5
+ import gradio as gr
6
+
7
+ history = [
8
+ gr.ChatMessage(role="assistant", content="How can I help you?"),
9
+ gr.ChatMessage(role="user", content="Can you make me a plot of quarterly sales?"),
10
+ gr.ChatMessage(role="assistant", content="I am happy to provide you that report and plot.")
11
+ ]
12
+
13
+ with gr.Blocks() as demo:
14
+ gr.Chatbot(history)
15
+
16
+ demo.launch()
ui/gradio_components.py CHANGED
@@ -1,6 +1,7 @@
1
  import gradio as gr
2
  from langchain_core.messages import HumanMessage, AIMessage
3
  from core.rag_agent import RAGAgent
 
4
 
5
  # Initialize components
6
  rag_agent = None
@@ -20,12 +21,14 @@ def chat_with_agent(message, history):
20
  try:
21
  agent = initialize_agent()
22
 
23
- # Convert Gradio history format to LangChain messages
24
  messages = []
25
- for user_msg, assistant_msg in history:
26
- messages.append(HumanMessage(content=user_msg))
27
- if assistant_msg:
28
- messages.append(AIMessage(content=assistant_msg))
 
 
29
 
30
  # Add current user message
31
  messages.append(HumanMessage(content=message))
@@ -53,20 +56,28 @@ def chat_with_agent(message, history):
53
  rag_method = result.get("rag_method", "UNKNOWN")
54
  response_with_metadata = f"{response}\n\n*[Source: {rag_method}]*"
55
 
56
- # Return history in Gradio's format [[user, bot], [user, bot], ...]
57
- new_history = history + [[message, response_with_metadata]]
 
 
 
58
  return new_history
59
  else:
60
- new_history = history + [[message, "⚠️ No response generated. Please try again."]]
 
 
 
61
  return new_history
62
 
63
  except Exception as e:
64
  error_msg = f"❌ Error: {str(e)}"
65
  print(f"Chat error: {e}")
66
- import traceback
67
  traceback.print_exc()
68
 
69
- new_history = history + [[message, error_msg]]
 
 
 
70
  return new_history
71
 
72
  def reset_conversation():
@@ -134,7 +145,7 @@ def create_gradio_ui():
134
 
135
  # Event handlers
136
  def submit_message(message, history):
137
- """Handle message submission with status update"""
138
  if not message.strip():
139
  return history, ""
140
 
 
1
  import gradio as gr
2
  from langchain_core.messages import HumanMessage, AIMessage
3
  from core.rag_agent import RAGAgent
4
+ import traceback
5
 
6
  # Initialize components
7
  rag_agent = None
 
21
  try:
22
  agent = initialize_agent()
23
 
24
+ # Convert Gradio history format (dict with role/content) to LangChain messages
25
  messages = []
26
+ if history:
27
+ for msg_dict in history:
28
+ if msg_dict["role"] == "user":
29
+ messages.append(HumanMessage(content=msg_dict["content"]))
30
+ elif msg_dict["role"] == "assistant":
31
+ messages.append(AIMessage(content=msg_dict["content"]))
32
 
33
  # Add current user message
34
  messages.append(HumanMessage(content=message))
 
56
  rag_method = result.get("rag_method", "UNKNOWN")
57
  response_with_metadata = f"{response}\n\n*[Source: {rag_method}]*"
58
 
59
+ # Return history in Gradio's dict format
60
+ new_history = history + [
61
+ {"role": "user", "content": message},
62
+ {"role": "assistant", "content": response_with_metadata}
63
+ ]
64
  return new_history
65
  else:
66
+ new_history = history + [
67
+ {"role": "user", "content": message},
68
+ {"role": "assistant", "content": "⚠️ No response generated. Please try again."}
69
+ ]
70
  return new_history
71
 
72
  except Exception as e:
73
  error_msg = f"❌ Error: {str(e)}"
74
  print(f"Chat error: {e}")
 
75
  traceback.print_exc()
76
 
77
+ new_history = history + [
78
+ {"role": "user", "content": message},
79
+ {"role": "assistant", "content": error_msg}
80
+ ]
81
  return new_history
82
 
83
  def reset_conversation():
 
145
 
146
  # Event handlers
147
  def submit_message(message, history):
148
+ """Handle message submission"""
149
  if not message.strip():
150
  return history, ""
151