Yordann commited on
Commit
af781fa
·
verified ·
1 Parent(s): cb3974f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -17
app.py CHANGED
@@ -1,4 +1,3 @@
1
- import requests
2
  import gradio as gr
3
  from transformers import pipeline
4
  from langdetect import detect
@@ -67,27 +66,34 @@ Give simple, friendly, clear advice:
67
  """
68
 
69
  # Generate response
70
- def chatbot(user_input, history, chat_counter):
71
- from langdetect import detect
72
 
73
- lang = detect(user_input)
74
  prompt = user_input
75
 
76
- api_response = requests.post(
77
- "https://chatbot-masterbrand.onrender.com/chat",
78
- json={"prompt": prompt}
79
- )
80
-
81
- if api_response.status_code == 200:
82
- result = api_response.json()
83
- response_text = result.get("response", "⚠️ No response from backend.")
84
- else:
85
- response_text = f"⚠️ Error from backend: {api_response.status_code}"
86
-
 
 
 
 
 
 
 
 
87
  if lang == "bg":
88
  response_text = en_to_bg(response_text)[0]["translation_text"]
89
 
90
- # Format for type="messages"
91
  history.append({"role": "user", "content": user_input})
92
  history.append({"role": "assistant", "content": response_text})
93
 
@@ -95,7 +101,6 @@ def chatbot(user_input, history, chat_counter):
95
 
96
  return history, history, chat_counter, "✅ Success", gr.update(value=''), gr.update(interactive=True)
97
 
98
-
99
  def reset_textbox():
100
  return gr.update(value='', interactive=False), gr.update(interactive=False)
101
 
 
 
1
  import gradio as gr
2
  from transformers import pipeline
3
  from langdetect import detect
 
66
  """
67
 
68
  # Generate response
69
+ import requests
 
70
 
71
+ def respond(user_input, history, chat_counter, lang):
72
  prompt = user_input
73
 
74
+ try:
75
+ # Send prompt to your backend
76
+ api_response = requests.post(
77
+ "https://chatbot-masterbrand.onrender.com/chat",
78
+ json={"prompt": prompt},
79
+ timeout=20
80
+ )
81
+
82
+ if api_response.status_code == 200:
83
+ result = api_response.json()
84
+ # result["response"] should contain the generated reply
85
+ response_text = result.get("response", "⚠️ No response from backend.")
86
+ else:
87
+ response_text = f"⚠️ Error from backend: {api_response.status_code}"
88
+
89
+ except Exception as e:
90
+ response_text = f"⚠️ Exception: {str(e)}"
91
+
92
+ # Optional: translate error to Bulgarian if needed
93
  if lang == "bg":
94
  response_text = en_to_bg(response_text)[0]["translation_text"]
95
 
96
+ # Add to chat history
97
  history.append({"role": "user", "content": user_input})
98
  history.append({"role": "assistant", "content": response_text})
99
 
 
101
 
102
  return history, history, chat_counter, "✅ Success", gr.update(value=''), gr.update(interactive=True)
103
 
 
104
  def reset_textbox():
105
  return gr.update(value='', interactive=False), gr.update(interactive=False)
106