hareballak commited on
Commit
fbaa611
·
verified ·
1 Parent(s): 34a23c4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -10
app.py CHANGED
@@ -73,30 +73,48 @@ def get_bot_response(query):
73
  best_idx = torch.argmax(scores)
74
 
75
  top_qa = qa_data[best_idx]
76
- prompt = f"""நீ ஒரு அறிவாளியான தமிழ் உதவியாளர்.
77
 
 
78
  தகவல்கள்:
79
  கேள்வி: {top_qa['question']}
80
  பதில்: {top_qa['answer']}
81
-
82
  மேலே உள்ள தகவல்களைப் பயன்படுத்தி, தெளிவான மற்றும் சுருக்கமான பதிலை வழங்கவும்.
83
-
84
  உயர்கட்ட கேள்வி: {query}
85
  பதில்:"""
86
 
87
- # Use LLaMA for generating the refined response
88
  headers = {"Authorization": f"Bearer {HF_API_TOKEN}"}
89
  payload = {
90
  "inputs": prompt,
91
- "parameters": {"temperature": 0.7, "max_new_tokens": 150, "return_full_text": False},
 
 
 
 
92
  }
 
 
93
  response = requests.post(LLAMA_API_URL, headers=headers, json=payload)
94
- result = response.json()
95
 
96
- if isinstance(result, list) and "generated_text" in result[0]:
97
- return result[0]["generated_text"]
98
- else:
99
- return "மன்னிக்கவும், நான் இந்த கேள்விக்கு பதில் தர முடியவில்லை."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
100
 
101
  # Gradio interface function
102
  def chatbot(audio, message, system_message, max_tokens, temperature, top_p):
 
73
  best_idx = torch.argmax(scores)
74
 
75
  top_qa = qa_data[best_idx]
 
76
 
77
+ prompt = f"""நீ ஒரு அறிவாளியான தமிழ் உதவியாளர்.
78
  தகவல்கள்:
79
  கேள்வி: {top_qa['question']}
80
  பதில்: {top_qa['answer']}
 
81
  மேலே உள்ள தகவல்களைப் பயன்படுத்தி, தெளிவான மற்றும் சுருக்கமான பதிலை வழங்கவும்.
 
82
  உயர்கட்ட கேள்வி: {query}
83
  பதில்:"""
84
 
 
85
  headers = {"Authorization": f"Bearer {HF_API_TOKEN}"}
86
  payload = {
87
  "inputs": prompt,
88
+ "parameters": {
89
+ "temperature": 0.7,
90
+ "max_new_tokens": 150,
91
+ "return_full_text": False
92
+ },
93
  }
94
+
95
+ # Post request
96
  response = requests.post(LLAMA_API_URL, headers=headers, json=payload)
 
97
 
98
+ # Sometimes inference is slow Wait for result
99
+ start_time = time.time()
100
+ max_wait_seconds = 180 # 💬 wait up to 3 minutes if necessary
101
+ while True:
102
+ try:
103
+ result = response.json()
104
+
105
+ if isinstance(result, list) and "generated_text" in result[0]:
106
+ return result[0]["generated_text"]
107
+ elif "error" in result and "loading" in result["error"].lower():
108
+ print("⏳ Model is loading, waiting 10 seconds...")
109
+ time.sleep(10)
110
+ else:
111
+ return "மன்னிக்கவும், நான் இந்த கேள்விக்கு பதில் தர முடியவில்லை."
112
+
113
+ except Exception as e:
114
+ if time.time() - start_time > max_wait_seconds:
115
+ return f"Error: Timeout while waiting for model prediction after {max_wait_seconds} seconds."
116
+ print(f"Waiting for model to respond... {str(e)}")
117
+ time.sleep(5) # wait 5 seconds before retry
118
 
119
  # Gradio interface function
120
  def chatbot(audio, message, system_message, max_tokens, temperature, top_p):