rbbist commited on
Commit
9a9b32f
·
verified ·
1 Parent(s): bc223c1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -7
app.py CHANGED
@@ -16,8 +16,9 @@ except Exception as e:
16
 
17
  # Load the model and tokenizer with fallback to FLAN-T5-Small
18
  model_name = "Qwen/Qwen2-0.5B-Instruct"
19
- fallback_model = "google/flan-t5-small" # Smaller model (~250 MB quantized)
20
  tokenizer = AutoTokenizer.from_pretrained(model_name)
 
21
 
22
  try:
23
  model = AutoModelForCausalLM.from_pretrained(
@@ -29,12 +30,13 @@ try:
29
  print(f"Successfully loaded {model_name}.")
30
  except Exception as e:
31
  print(f"Failed to load {model_name} with quantization: {e}. Falling back to {fallback_model}.")
 
32
  model = AutoModelForCausalLM.from_pretrained(
33
  fallback_model,
34
  device_map="cpu",
35
  low_cpu_mem_usage=True,
36
  )
37
- tokenizer = AutoTokenizer.from_pretrained(fallback_model) # Update tokenizer for fallback
38
 
39
  def generate_llm_response(message):
40
  """Generate response using the loaded model with multilingual prompting"""
@@ -44,16 +46,16 @@ def generate_llm_response(message):
44
  # Detect if the input is in Nepali
45
  is_nepali = bool(re.search(r'[\u0900-\u097F]', message))
46
 
47
- # Craft a prompt based on language detection
48
  if is_nepali:
49
- prompt = f"तपाईं एक नेपाली च्याटबोट हुनुहुन्छ। प्रयोगकर्ताले भनेको कराको जवाफ नेपालीमा दिनुहोस्: {message}"
50
  else:
51
- prompt = f"You are a friendly chatbot that can respond in English or Nepali. Respond to the user's message: {message}"
52
 
53
  try:
54
  # Tokenize and generate response
55
  inputs = tokenizer(prompt, return_tensors="pt").to("cpu")
56
- outputs = model.generate(**inputs, max_new_tokens=64, temperature=0.7, do_sample=True)
57
  response = tokenizer.decode(outputs[0], skip_special_tokens=True)
58
 
59
  # Post-process to ensure a complete sentence
@@ -61,6 +63,10 @@ def generate_llm_response(message):
61
  if response and not response.endswith(('.', '!', '?')):
62
  response += "।" if is_nepali else "."
63
 
 
 
 
 
64
  return response if response else "Sorry, I couldn't generate a response."
65
 
66
  except Exception as e:
@@ -107,7 +113,7 @@ css = """
107
  border-radius: 25px !important;
108
  }
109
  .input-container input {
110
- color: #ffffff !important;
111
  background: transparent !important;
112
  }
113
  .gradio-chatbot {
 
16
 
17
  # Load the model and tokenizer with fallback to FLAN-T5-Small
18
  model_name = "Qwen/Qwen2-0.5B-Instruct"
19
+ fallback_model = "google/flan-t5-small"
20
  tokenizer = AutoTokenizer.from_pretrained(model_name)
21
+ loaded_model_name = model_name # Track loaded model
22
 
23
  try:
24
  model = AutoModelForCausalLM.from_pretrained(
 
30
  print(f"Successfully loaded {model_name}.")
31
  except Exception as e:
32
  print(f"Failed to load {model_name} with quantization: {e}. Falling back to {fallback_model}.")
33
+ loaded_model_name = fallback_model
34
  model = AutoModelForCausalLM.from_pretrained(
35
  fallback_model,
36
  device_map="cpu",
37
  low_cpu_mem_usage=True,
38
  )
39
+ tokenizer = AutoTokenizer.from_pretrained(fallback_model)
40
 
41
  def generate_llm_response(message):
42
  """Generate response using the loaded model with multilingual prompting"""
 
46
  # Detect if the input is in Nepali
47
  is_nepali = bool(re.search(r'[\u0900-\u097F]', message))
48
 
49
+ # Craft a detailed prompt based on language detection
50
  if is_nepali:
51
+ prompt = f"तपाईं एक नेपाली च्याटबोट हुनुहुन्छ जसले नेपालीमा प्राकृतिक र पूर्ण जवाफ दिन्छ। प्रयोगकर्ताले भन्नभय: '{message}'। जवाफ दिनुहोस्:"
52
  else:
53
+ prompt = f"You are a friendly chatbot that responds naturally in English or Nepali. The user said: '{message}'. Please respond:"
54
 
55
  try:
56
  # Tokenize and generate response
57
  inputs = tokenizer(prompt, return_tensors="pt").to("cpu")
58
+ outputs = model.generate(**inputs, max_new_tokens=128, temperature=0.7, do_sample=True) # Increased tokens for completeness
59
  response = tokenizer.decode(outputs[0], skip_special_tokens=True)
60
 
61
  # Post-process to ensure a complete sentence
 
63
  if response and not response.endswith(('.', '!', '?')):
64
  response += "।" if is_nepali else "."
65
 
66
+ # Fallback for inadequate Nepali response
67
+ if is_nepali and len(response.split()) < 3: # If response is too short
68
+ return "माफ गर्नुहोस्, मलाई थप जानकारी चाहिए। तपाईंले के बारेमा कुरा गर्न चाहनुहुन्छ?"
69
+
70
  return response if response else "Sorry, I couldn't generate a response."
71
 
72
  except Exception as e:
 
113
  border-radius: 25px !important;
114
  }
115
  .input-container input {
116
+ color: #1e1e1e !important;
117
  background: transparent !important;
118
  }
119
  .gradio-chatbot {