ianeksdi commited on
Commit
39432c2
·
verified ·
1 Parent(s): 72e07db

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -15
app.py CHANGED
@@ -6,10 +6,6 @@ from deep_translator import GoogleTranslator
6
  client = InferenceClient("mistralai/Mistral-Nemo-Instruct-2407")
7
 
8
  def get_internet_data(query: str) -> str:
9
- """
10
- Uses DuckDuckGo's Instant Answer API to fetch search data.
11
- Returns a short summary of the query result.
12
- """
13
  url = "https://api.duckduckgo.com"
14
  params = {
15
  "q": query,
@@ -29,11 +25,7 @@ def get_internet_data(query: str) -> str:
29
  return result
30
 
31
  def translate_to_bisaya(text: str) -> str:
32
- """
33
- Translates the given text to Bisaya (Cebuano) using deep-translator.
34
- """
35
  try:
36
- # 'ceb' is the ISO 639-3 code for Cebuano
37
  return GoogleTranslator(source='auto', target='ceb').translate(text)
38
  except Exception:
39
  return text
@@ -66,8 +58,8 @@ def respond(message, history: list[tuple[str, str]]):
66
  )
67
  messages.append({"role": "user", "content": enriched_message})
68
 
69
- response = ""
70
- previous_response = ""
71
  for token_message in client.chat_completion(
72
  messages,
73
  max_tokens=max_tokens,
@@ -78,13 +70,14 @@ def respond(message, history: list[tuple[str, str]]):
78
  token = token_message.choices[0].delta.get("content", "")
79
  if not token:
80
  break
81
- response += token
82
- if response != previous_response:
83
- yield response
84
- previous_response = response
85
- if len(response) > 3000:
86
  break
87
 
 
 
 
 
88
  demo = gr.ChatInterface(respond)
89
 
90
  if __name__ == "__main__":
 
6
  client = InferenceClient("mistralai/Mistral-Nemo-Instruct-2407")
7
 
8
  def get_internet_data(query: str) -> str:
 
 
 
 
9
  url = "https://api.duckduckgo.com"
10
  params = {
11
  "q": query,
 
25
  return result
26
 
27
  def translate_to_bisaya(text: str) -> str:
 
 
 
28
  try:
 
29
  return GoogleTranslator(source='auto', target='ceb').translate(text)
30
  except Exception:
31
  return text
 
58
  )
59
  messages.append({"role": "user", "content": enriched_message})
60
 
61
+ # Collect the complete response from the model
62
+ full_response = ""
63
  for token_message in client.chat_completion(
64
  messages,
65
  max_tokens=max_tokens,
 
70
  token = token_message.choices[0].delta.get("content", "")
71
  if not token:
72
  break
73
+ full_response += token
74
+ if len(full_response) > 3000:
 
 
 
75
  break
76
 
77
+ # Translate the complete response to Bisaya before yielding it.
78
+ final_response = translate_to_bisaya(full_response)
79
+ yield final_response
80
+
81
  demo = gr.ChatInterface(respond)
82
 
83
  if __name__ == "__main__":