SamiKoen Claude commited on
Commit
121b113
·
1 Parent(s): cad0b30

Fix Gradio errors and update to messages format

Browse files

- Removed invalid 'every' parameter from demo.load
- Updated Chatbot to use 'messages' format instead of deprecated 'tuples'
- Updated respond function to handle messages format (role/content structure)
- Fixed streaming response handling for new format

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>

Files changed (1) hide show
  1. app.py +20 -15
app.py CHANGED
@@ -1072,7 +1072,7 @@ with gr.Blocks(css=custom_css, theme="soft", title="Trek Asistanı", head=storag
1072
 
1073
  # LocalStorage fonksiyonu kaldırıldı
1074
 
1075
- chatbot = gr.Chatbot(height=600, elem_id="chatbot", show_label=False, type="tuples")
1076
 
1077
  msg = gr.Textbox(
1078
  placeholder="Trek bisikletleri hakkında soru sorun...",
@@ -1091,17 +1091,14 @@ with gr.Blocks(css=custom_css, theme="soft", title="Trek Asistanı", head=storag
1091
  if chat_history is None:
1092
  chat_history = []
1093
 
1094
- # Kullanıcı mesajını ekle ve hemen yield et
1095
- chat_history.append((message, None))
1096
  yield "", chat_history
1097
 
1098
- # Chat history'yi chatbot_fn için uygun formata çevir (son mesaj hariç)
1099
  formatted_history = []
1100
- if len(chat_history) > 1: # Son mesajı hariç tut
1101
- for user_msg, bot_msg in chat_history[:-1]:
1102
- if bot_msg: # Sadece tamamlanmış mesajlar
1103
- formatted_history.append({"role": "user", "content": user_msg})
1104
- formatted_history.append({"role": "assistant", "content": bot_msg})
1105
 
1106
  try:
1107
  # Enhanced chatbot fonksiyonunu çağır (image=None)
@@ -1111,8 +1108,13 @@ with gr.Blocks(css=custom_css, theme="soft", title="Trek Asistanı", head=storag
1111
  response = ""
1112
  for partial in response_generator:
1113
  response = partial
1114
- # Son mesajı güncelle ve yield et
1115
- chat_history[-1] = (message, response)
 
 
 
 
 
1116
  yield "", chat_history
1117
 
1118
  # Konuşmayı kaydet
@@ -1124,8 +1126,11 @@ with gr.Blocks(css=custom_css, theme="soft", title="Trek Asistanı", head=storag
1124
  except Exception as e:
1125
  error_msg = f"Üzgünüm, bir hata oluştu: {str(e)}"
1126
  print(f"Chat error: {e}")
1127
- # Hata mesajıyla güncelle
1128
- chat_history[-1] = (message, error_msg)
 
 
 
1129
  yield "", chat_history
1130
 
1131
  msg.submit(respond, [msg, chatbot], [msg, chatbot], show_progress=True)
@@ -1203,8 +1208,8 @@ with gr.Blocks(css=custom_css, theme="soft", title="Trek Asistanı", head=storag
1203
  elem_id="json_data_container"
1204
  )
1205
 
1206
- # Update JSON HTML every 5 seconds
1207
- demo.load(create_json_html, outputs=json_html, every=5)
1208
 
1209
  # API endpoints for dashboard
1210
  def get_all_conversations():
 
1072
 
1073
  # LocalStorage fonksiyonu kaldırıldı
1074
 
1075
+ chatbot = gr.Chatbot(height=600, elem_id="chatbot", show_label=False, type="messages")
1076
 
1077
  msg = gr.Textbox(
1078
  placeholder="Trek bisikletleri hakkında soru sorun...",
 
1091
  if chat_history is None:
1092
  chat_history = []
1093
 
1094
+ # Messages format için kullanıcı mesajını ekle
1095
+ chat_history.append({"role": "user", "content": message})
1096
  yield "", chat_history
1097
 
1098
+ # Chat history'yi chatbot_fn için uygun formata çevir
1099
  formatted_history = []
1100
+ for msg in chat_history[:-1]: # Son mesajı hariç tut
1101
+ formatted_history.append(msg)
 
 
 
1102
 
1103
  try:
1104
  # Enhanced chatbot fonksiyonunu çağır (image=None)
 
1108
  response = ""
1109
  for partial in response_generator:
1110
  response = partial
1111
+ # Bot cevabını ekle veya güncelle
1112
+ # Eğer son mesaj user ise, bot mesajı ekle
1113
+ if chat_history[-1]["role"] == "user":
1114
+ chat_history.append({"role": "assistant", "content": response})
1115
+ else:
1116
+ # Son bot mesajını güncelle
1117
+ chat_history[-1]["content"] = response
1118
  yield "", chat_history
1119
 
1120
  # Konuşmayı kaydet
 
1126
  except Exception as e:
1127
  error_msg = f"Üzgünüm, bir hata oluştu: {str(e)}"
1128
  print(f"Chat error: {e}")
1129
+ # Hata mesajı ekle
1130
+ if chat_history[-1]["role"] == "user":
1131
+ chat_history.append({"role": "assistant", "content": error_msg})
1132
+ else:
1133
+ chat_history[-1]["content"] = error_msg
1134
  yield "", chat_history
1135
 
1136
  msg.submit(respond, [msg, chatbot], [msg, chatbot], show_progress=True)
 
1208
  elem_id="json_data_container"
1209
  )
1210
 
1211
+ # Update JSON HTML on page load
1212
+ demo.load(create_json_html, outputs=json_html)
1213
 
1214
  # API endpoints for dashboard
1215
  def get_all_conversations():