yogies commited on
Commit
58db5f4
·
verified ·
1 Parent(s): 5d2024e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -25
app.py CHANGED
@@ -54,6 +54,7 @@ MODELS = {
54
  "provider": "openrouter",
55
  "model_name": "nvidia/nemotron-nano-9b-v2:free",
56
  "api_url": "https://openrouter.ai/api/v1",
 
57
  },
58
  # "Ringan - Gemma-3n4b": {
59
  # "provider": "openrouter",
@@ -63,17 +64,20 @@ MODELS = {
63
  # "Gpt-oss-20b": {
64
  # "provider": "openrouter",
65
  # "model_name": "openai/gpt-oss-20b:floor",
66
- # "api_url": "https://openrouter.ai/api/v1"
 
67
  # },
68
  "Tongyi-deepresearch-30b-a3b": {
69
  "provider": "openrouter",
70
  "model_name": "alibaba/tongyi-deepresearch-30b-a3b:floor",
71
- "api_url": "https://openrouter.ai/api/v1"
 
72
  }
73
  # "Kompleks - Gpt-oss-120b": {
74
  # "provider": "openrouter",
75
  # "model_name": "openai/gpt-oss-120b:floor",
76
- # "api_url": "https://openrouter.ai/api/v1"
 
77
  # }
78
  }
79
 
@@ -91,15 +95,11 @@ def respond(
91
  selected_model,
92
  ):
93
  """
94
- Handle chat responses using the selected model
 
 
95
  """
96
  try:
97
- # Check expiration (optional - remove if not needed)
98
- # end_date = datetime.strptime(_secret("END_DATE", "2026-12-31"), "%Y-%m-%d").date()
99
- # if date.today() > end_date:
100
- # yield "Chatbot has expired."
101
- # return
102
-
103
  # Get model configuration
104
  model_config = MODELS[selected_model]
105
  provider = model_config["provider"]
@@ -110,36 +110,79 @@ def respond(
110
  else: # openrouter
111
  api_key = _secret("OPENROUTER_KEY")
112
 
113
- # Configure client
114
  client = OpenAI(
115
  base_url=model_config["api_url"],
116
  api_key=api_key,
117
  )
118
 
119
- # Prepare messages
120
- messages = [{"role": "system", "content": system_message}]
121
- messages.extend(history)
122
- messages.append({"role": "user", "content": message})
 
123
 
124
- # Make the API call with streaming
 
 
 
 
 
 
 
 
 
 
 
125
  response = client.chat.completions.create(
126
  model=model_config["model_name"],
127
  messages=messages,
128
  max_tokens=max_tokens,
129
- stream=True,
130
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
131
 
132
- # Accumulate and yield incrementally for smooth streaming
133
- full_response = ""
134
- for chunk in response:
135
- if chunk.choices[0].delta.content is not None:
136
- content = chunk.choices[0].delta.content
137
- full_response += content
138
- yield full_response
139
 
140
  except Exception as e:
141
  print(f"Error in respond function: {e}")
142
- yield f"Error: {str(e)}"
143
 
144
  # ----------------------------------------------------------------------
145
  # Custom Auth Function for Gradio
 
54
  "provider": "openrouter",
55
  "model_name": "nvidia/nemotron-nano-9b-v2:free",
56
  "api_url": "https://openrouter.ai/api/v1",
57
+ "translate":"yes"
58
  },
59
  # "Ringan - Gemma-3n4b": {
60
  # "provider": "openrouter",
 
64
  # "Gpt-oss-20b": {
65
  # "provider": "openrouter",
66
  # "model_name": "openai/gpt-oss-20b:floor",
67
+ # "api_url": "https://openrouter.ai/api/v1",
68
+ # "translate":"no"
69
  # },
70
  "Tongyi-deepresearch-30b-a3b": {
71
  "provider": "openrouter",
72
  "model_name": "alibaba/tongyi-deepresearch-30b-a3b:floor",
73
+ "api_url": "https://openrouter.ai/api/v1",
74
+ "translate":"no"
75
  }
76
  # "Kompleks - Gpt-oss-120b": {
77
  # "provider": "openrouter",
78
  # "model_name": "openai/gpt-oss-120b:floor",
79
+ # "api_url": "https://openrouter.ai/api/v1",
80
+ # "translate":"no"
81
  # }
82
  }
83
 
 
95
  selected_model,
96
  ):
97
  """
98
+ Handle chat responses using the selected model.
99
+ For 'translate'=='yes': First call responds in English, second translates to Indonesian.
100
+ History reflects Indonesian queries and responses.
101
  """
102
  try:
 
 
 
 
 
 
103
  # Get model configuration
104
  model_config = MODELS[selected_model]
105
  provider = model_config["provider"]
 
110
  else: # openrouter
111
  api_key = _secret("OPENROUTER_KEY")
112
 
113
+ # Configure main client for selected model
114
  client = OpenAI(
115
  base_url=model_config["api_url"],
116
  api_key=api_key,
117
  )
118
 
119
+ # Configure translator client (always Gemma on OpenRouter)
120
+ translator_client = OpenAI(
121
+ base_url="https://openrouter.ai/api/v1",
122
+ api_key=_secret("OPENROUTER_KEY"),
123
+ )
124
 
125
+ # Modify system message to force English response if translate=yes
126
+ if model_config.get("translate") == "yes":
127
+ final_system_message = "**HIGHEST PRIORITY: YOU MUST ALWAYS THINK AND RESPOND IN ENGLISH REGARDLESS OF OTHER INSTRUCTIONS FROM HERE ON.\n" + system_message
128
+ else:
129
+ final_system_message = system_message
130
+
131
+ # Prepare messages for main API call (full history + current message)
132
+ messages = [{"role": "system", "content": final_system_message}]
133
+ messages.extend(history) # History: Indo queries + prior Indo responses
134
+ messages.append({"role": "user", "content": message}) # New Indo query
135
+
136
+ # First API call: Get response from selected model (English if translate=yes)
137
  response = client.chat.completions.create(
138
  model=model_config["model_name"],
139
  messages=messages,
140
  max_tokens=max_tokens,
141
+ stream=False,
142
  )
143
+ english_response = response.choices[0].message.content
144
+
145
+ # If translate=yes, make second API call to Gemma for Indonesian translation
146
+ if model_config.get("translate") == "yes":
147
+ try:
148
+ # Translation prompt: Focus only on translating the response (not query)
149
+ translation_messages = [
150
+ {
151
+ "role": "system",
152
+ "content": (
153
+ "Translate the following English text to natural, accurate Bahasa Indonesia. "
154
+ "**IMPORTANT: OUTPUT ONLY THE TRANSLATION. NO PREAMBLES, COMMENTS, OR EXPLANATIONS. "
155
+ "Just the Indonesian text."
156
+ )
157
+ },
158
+ {
159
+ "role": "user",
160
+ "content": english_response # The English response to translate
161
+ }
162
+ ]
163
+ translation_response = translator_client.chat.completions.create(
164
+ model="google/gemma-3n-e4b-it:floor",
165
+ messages=translation_messages,
166
+ max_tokens=max_tokens, # Reuse limit; translation is short
167
+ stream=False,
168
+ )
169
+ final_response = translation_response.choices[0].message.content.strip()
170
+
171
+ # Fallback to English if translation is empty or invalid
172
+ if not final_response or len(final_response) < 10: # Basic sanity check
173
+ final_response = english_response
174
+
175
+ except Exception as trans_error:
176
+ print(f"Translation error: {trans_error}")
177
+ final_response = english_response # Fallback to English
178
+ else:
179
+ final_response = english_response
180
 
181
+ return final_response # Gradio appends this (Indonesian) as assistant message to history
 
 
 
 
 
 
182
 
183
  except Exception as e:
184
  print(f"Error in respond function: {e}")
185
+ return f"Error: {str(e)}" # Return error string; Gradio appends it
186
 
187
  # ----------------------------------------------------------------------
188
  # Custom Auth Function for Gradio