Ali2206 commited on
Commit
e014e82
·
verified ·
1 Parent(s): a6601ec

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -19
app.py CHANGED
@@ -68,9 +68,8 @@ class TxAgentApplication:
68
 
69
  def chat(self, message, chat_history):
70
  if not self.is_initialized:
71
- yield "Error: Please initialize the model first"
72
- return
73
-
74
  try:
75
  # Convert to messages format
76
  messages = []
@@ -79,8 +78,8 @@ class TxAgentApplication:
79
  messages.append({"role": "assistant", "content": assistant})
80
  messages.append({"role": "user", "content": message})
81
 
82
- # Stream response
83
- full_response = ""
84
  for chunk in self.agent.run_gradio_chat(
85
  messages,
86
  temperature=0.3,
@@ -90,11 +89,11 @@ class TxAgentApplication:
90
  conversation=[],
91
  max_round=30
92
  ):
93
- full_response += chunk
94
- yield [(message, full_response)]
95
-
96
  except Exception as e:
97
- yield [(message, f"Error: {str(e)}")]
98
 
99
  # ========== Gradio Interface ==========
100
  def create_interface():
@@ -108,21 +107,24 @@ def create_interface():
108
  init_btn = gr.Button("Initialize TxAgent", variant="primary")
109
  init_status = gr.Textbox(label="Status", interactive=False)
110
 
111
- # Chat Interface (using modern messages format)
112
  chatbot = gr.Chatbot(
113
  height=600,
114
  label="Conversation",
 
115
  avatar_images=(
116
- "https://example.com/user.png", # User avatar
117
- "https://example.com/bot.png" # Bot avatar
118
- )
 
119
  )
120
 
121
  with gr.Row():
122
  msg = gr.Textbox(
123
  label="Your Question",
124
  placeholder="Ask about drug interactions or treatments...",
125
- scale=4
 
126
  )
127
  submit_btn = gr.Button("Submit", variant="primary", scale=1)
128
 
@@ -130,7 +132,8 @@ def create_interface():
130
  gr.Examples(
131
  examples=EXAMPLE_QUESTIONS,
132
  inputs=msg,
133
- label="Try these examples:"
 
134
  )
135
 
136
  gr.Markdown(UI_CONFIG['disclaimer'])
@@ -144,13 +147,15 @@ def create_interface():
144
  msg.submit(
145
  app.chat,
146
  [msg, chatbot],
147
- [chatbot]
 
148
  )
149
 
150
  submit_btn.click(
151
  app.chat,
152
  [msg, chatbot],
153
- [chatbot]
 
154
  ).then(
155
  lambda: "", None, msg
156
  )
@@ -162,9 +167,9 @@ if __name__ == "__main__":
162
  interface = create_interface()
163
 
164
  # Correct launch configuration
 
165
  interface.launch(
166
  server_name="0.0.0.0",
167
  server_port=7860,
168
- share=True,
169
- enable_queue=True # Enable queue without deprecated parameters
170
  )
 
68
 
69
  def chat(self, message, chat_history):
70
  if not self.is_initialized:
71
+ return chat_history + [(message, "Error: Please initialize the model first")]
72
+
 
73
  try:
74
  # Convert to messages format
75
  messages = []
 
78
  messages.append({"role": "assistant", "content": assistant})
79
  messages.append({"role": "user", "content": message})
80
 
81
+ # Get response
82
+ response = ""
83
  for chunk in self.agent.run_gradio_chat(
84
  messages,
85
  temperature=0.3,
 
89
  conversation=[],
90
  max_round=30
91
  ):
92
+ response += chunk
93
+
94
+ return chat_history + [(message, response)]
95
  except Exception as e:
96
+ return chat_history + [(message, f"Error: {str(e)}")]
97
 
98
  # ========== Gradio Interface ==========
99
  def create_interface():
 
107
  init_btn = gr.Button("Initialize TxAgent", variant="primary")
108
  init_status = gr.Textbox(label="Status", interactive=False)
109
 
110
+ # Chat Interface using modern messages format
111
  chatbot = gr.Chatbot(
112
  height=600,
113
  label="Conversation",
114
+ show_label=True,
115
  avatar_images=(
116
+ None, # User avatar (None for default)
117
+ None # Bot avatar (None for default)
118
+ ),
119
+ show_copy_button=True
120
  )
121
 
122
  with gr.Row():
123
  msg = gr.Textbox(
124
  label="Your Question",
125
  placeholder="Ask about drug interactions or treatments...",
126
+ scale=4,
127
+ container=False
128
  )
129
  submit_btn = gr.Button("Submit", variant="primary", scale=1)
130
 
 
132
  gr.Examples(
133
  examples=EXAMPLE_QUESTIONS,
134
  inputs=msg,
135
+ label="Try these examples:",
136
+ examples_per_page=3
137
  )
138
 
139
  gr.Markdown(UI_CONFIG['disclaimer'])
 
147
  msg.submit(
148
  app.chat,
149
  [msg, chatbot],
150
+ chatbot,
151
+ queue=True # Enable queue for this event
152
  )
153
 
154
  submit_btn.click(
155
  app.chat,
156
  [msg, chatbot],
157
+ chatbot,
158
+ queue=True # Enable queue for this event
159
  ).then(
160
  lambda: "", None, msg
161
  )
 
167
  interface = create_interface()
168
 
169
  # Correct launch configuration
170
+ interface.queue() # Enable queue globally
171
  interface.launch(
172
  server_name="0.0.0.0",
173
  server_port=7860,
174
+ share=True
 
175
  )