akashraut commited on
Commit
afb7b3f
·
verified ·
1 Parent(s): 2223d6d

Update streamlit_app.py

Browse files
Files changed (1) hide show
  1. streamlit_app.py +88 -40
streamlit_app.py CHANGED
@@ -63,23 +63,26 @@ class LangChainBot:
63
  model=model_name,
64
  device=device,
65
  torch_dtype=torch_dtype,
66
- max_new_tokens=150,
67
- repetition_penalty=1.2,
68
- trust_remote_code=True # Added this for compatibility
 
 
 
 
69
  )
70
 
71
  # Wrap in LangChain LLM
72
  llm = HuggingFacePipeline(pipeline=generator_pipeline)
73
 
74
  # Create prompt template
75
- template = """
76
- You are a helpful conversational AI. Respond to the user's message appropriately.
77
- Previous conversation:
78
- {history}
79
-
80
- Human: {input}
81
- Assistant:
82
- """
83
  prompt_template = PromptTemplate(
84
  input_variables=["history", "input"],
85
  template=template
@@ -109,8 +112,8 @@ class LangChainBot:
109
  def _load_translator(self, device):
110
  """Load the translator with fallback options."""
111
  translators_to_try = [
 
112
  "ai4bharat/indictrans2-indic-indic-1B",
113
- "Helsinki-NLP/opus-mt-en-hi", # Fallback for English-Hindi
114
  ]
115
 
116
  for translator_name in translators_to_try:
@@ -140,30 +143,41 @@ class LangChainBot:
140
  return text
141
 
142
  try:
143
- # Define language codes
144
- codes = {
145
  'english': 'eng_Latn',
146
  'hindi': 'hin_Deva',
147
  'tamil': 'tam_Taml',
148
  'telugu': 'tel_Telu'
149
  }
150
 
151
- if source_lang in codes and target_lang in codes:
152
- result = self.translator(
153
- text,
154
- src_lang=codes[source_lang],
155
- tgt_lang=codes[target_lang]
156
- )
157
- return result[0]['translation_text']
158
- else:
159
- # Fallback for simple English-Hindi translation
160
- if source_lang == 'english' and target_lang == 'hindi':
161
- result = self.translator(text)
162
- return result[0]['translation_text'] if result else text
 
 
 
 
 
 
 
 
 
 
 
163
 
164
  except Exception as e:
165
  logger.warning(f"Translation failed: {e}")
166
- st.warning(f"Translation failed, using original text. Error: {e}")
167
 
168
  return text
169
 
@@ -173,26 +187,53 @@ class LangChainBot:
173
  return "Error: The LangChain chain is not initialized. Please check the logs above."
174
 
175
  try:
176
- # Translate input to a common language if needed
177
- if input_lang != 'english':
178
- processed_message = self._translate(user_message, input_lang, 'english')
179
- else:
 
 
180
  processed_message = user_message
 
 
 
 
 
 
 
 
181
 
182
  # Generate response
183
- response = self.chain.run(processed_message)
184
 
185
- # Translate output if needed
186
- if output_lang != 'english':
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
187
  final_response = self._translate(response, 'english', output_lang)
 
 
188
  else:
189
- final_response = response
190
 
191
- return final_response
192
-
193
  except Exception as e:
194
  logger.error(f"Error generating response: {e}")
195
- return f"I apologize, but I encountered an error while processing your request: {str(e)}"
196
 
197
  # -----------------------------------------------------------------------------
198
  # STREAMLIT UI WITH BETTER ERROR HANDLING
@@ -222,7 +263,7 @@ if bot and bot.chain:
222
 
223
  st.markdown("---")
224
 
225
- # Language selection
226
  language_options = ["english", "hindi", "tamil", "telugu"]
227
  col1, col2 = st.columns(2)
228
 
@@ -237,9 +278,16 @@ if bot and bot.chain:
237
  output_lang = st.selectbox(
238
  "🗣️ Output Language",
239
  options=language_options,
240
- index=1,
241
  help="Select the language for the response"
242
  )
 
 
 
 
 
 
 
243
 
244
  # Chat interface
245
  st.markdown("### 💬 Chat Interface")
 
63
  model=model_name,
64
  device=device,
65
  torch_dtype=torch_dtype,
66
+ max_new_tokens=100, # Reduced from 150
67
+ repetition_penalty=1.5, # Increased from 1.2
68
+ do_sample=True,
69
+ temperature=0.7,
70
+ top_p=0.9,
71
+ no_repeat_ngram_size=3, # Prevent repetition
72
+ trust_remote_code=True
73
  )
74
 
75
  # Wrap in LangChain LLM
76
  llm = HuggingFacePipeline(pipeline=generator_pipeline)
77
 
78
  # Create prompt template
79
+ template = """You are a helpful AI assistant. Please provide a clear and concise response to the user's question.
80
+
81
+ Previous conversation:
82
+ {history}
83
+
84
+ User: {input}
85
+ Assistant:"""
 
86
  prompt_template = PromptTemplate(
87
  input_variables=["history", "input"],
88
  template=template
 
112
  def _load_translator(self, device):
113
  """Load the translator with fallback options."""
114
  translators_to_try = [
115
+ "Helsinki-NLP/opus-mt-en-hi", # More reliable fallback for English-Hindi
116
  "ai4bharat/indictrans2-indic-indic-1B",
 
117
  ]
118
 
119
  for translator_name in translators_to_try:
 
143
  return text
144
 
145
  try:
146
+ # Define language codes for indictrans2
147
+ indictrans_codes = {
148
  'english': 'eng_Latn',
149
  'hindi': 'hin_Deva',
150
  'tamil': 'tam_Taml',
151
  'telugu': 'tel_Telu'
152
  }
153
 
154
+ # Try indictrans2 format first
155
+ if source_lang in indictrans_codes and target_lang in indictrans_codes:
156
+ try:
157
+ result = self.translator(
158
+ text,
159
+ src_lang=indictrans_codes[source_lang],
160
+ tgt_lang=indictrans_codes[target_lang]
161
+ )
162
+ if result and len(result) > 0 and 'translation_text' in result[0]:
163
+ return result[0]['translation_text']
164
+ except Exception as e:
165
+ logger.warning(f"Indictrans2 translation failed: {e}")
166
+
167
+ # Fallback: Try simple pipeline format
168
+ try:
169
+ result = self.translator(text)
170
+ if result and len(result) > 0:
171
+ if 'translation_text' in result[0]:
172
+ return result[0]['translation_text']
173
+ elif 'generated_text' in result[0]:
174
+ return result[0]['generated_text']
175
+ except Exception as e:
176
+ logger.warning(f"Simple translation failed: {e}")
177
 
178
  except Exception as e:
179
  logger.warning(f"Translation failed: {e}")
180
+ # Don't show warning to user for every translation failure
181
 
182
  return text
183
 
 
187
  return "Error: The LangChain chain is not initialized. Please check the logs above."
188
 
189
  try:
190
+ # Clean the input message
191
+ user_message = user_message.strip()
192
+
193
+ # For now, let's work primarily in English to avoid translation issues
194
+ # Only translate if specifically needed and working
195
+ if input_lang == 'english':
196
  processed_message = user_message
197
+ else:
198
+ # Try translation, but fallback to original if it fails
199
+ translated = self._translate(user_message, input_lang, 'english')
200
+ processed_message = translated if translated != user_message else user_message
201
+
202
+ # Generate response with input validation
203
+ if len(processed_message.strip()) == 0:
204
+ return "I didn't receive a valid message. Please try again."
205
 
206
  # Generate response
207
+ response = self.chain.run(input=processed_message)
208
 
209
+ # Clean up the response
210
+ response = response.strip()
211
+
212
+ # Remove any repetitive patterns
213
+ words = response.split()
214
+ if len(words) > 10:
215
+ # Check for excessive repetition
216
+ word_counts = {}
217
+ for word in words:
218
+ word_counts[word] = word_counts.get(word, 0) + 1
219
+
220
+ # If any word appears more than 5 times, it's likely repetitive
221
+ max_count = max(word_counts.values()) if word_counts else 0
222
+ if max_count > 5:
223
+ # Generate a simple fallback response
224
+ response = f"I understand you said '{processed_message[:50]}...' Let me provide a helpful response to that."
225
+
226
+ # Translate output if needed and different from English
227
+ if output_lang != 'english' and output_lang != input_lang:
228
  final_response = self._translate(response, 'english', output_lang)
229
+ # If translation fails, return English response
230
+ return final_response if final_response != response else response
231
  else:
232
+ return response
233
 
 
 
234
  except Exception as e:
235
  logger.error(f"Error generating response: {e}")
236
+ return f"I apologize, but I encountered an error while processing your request. Please try rephrasing your message."
237
 
238
  # -----------------------------------------------------------------------------
239
  # STREAMLIT UI WITH BETTER ERROR HANDLING
 
263
 
264
  st.markdown("---")
265
 
266
+ # Language selection with helpful notes
267
  language_options = ["english", "hindi", "tamil", "telugu"]
268
  col1, col2 = st.columns(2)
269
 
 
278
  output_lang = st.selectbox(
279
  "🗣️ Output Language",
280
  options=language_options,
281
+ index=0, # Default to English for now
282
  help="Select the language for the response"
283
  )
284
+
285
+ # Show translation status
286
+ if not bot.translator:
287
+ st.info("ℹ️ Translation is currently limited. For best results, use English input and output.")
288
+ elif input_lang != 'english' or output_lang != 'english':
289
+ st.warning("⚠️ Translation is experimental. If you encounter issues, try using English.")
290
+
291
 
292
  # Chat interface
293
  st.markdown("### 💬 Chat Interface")