Spaces:
Sleeping
Sleeping
update app
Browse files
app.py
CHANGED
@@ -3,6 +3,7 @@ import torch
|
|
3 |
import gradio as gr
|
4 |
from gtts import gTTS
|
5 |
from langdetect import detect
|
|
|
6 |
|
7 |
model_name = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
|
8 |
|
@@ -13,18 +14,22 @@ def respond(user_input):
|
|
13 |
if not user_input:
|
14 |
return "Please ask something.", None
|
15 |
|
|
|
16 |
detected_lang = detect(user_input)
|
17 |
|
|
|
18 |
prompt = f"[INST] {user_input} [/INST]"
|
19 |
inputs = tokenizer(prompt, return_tensors="pt")
|
20 |
outputs = model.generate(**inputs, max_new_tokens=256, pad_token_id=tokenizer.eos_token_id)
|
21 |
|
22 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
23 |
|
24 |
-
#
|
25 |
tts = gTTS(text=response, lang='hi' if detected_lang == 'hi' else 'en')
|
26 |
-
|
27 |
-
|
|
|
|
|
28 |
|
29 |
iface = gr.Interface(
|
30 |
fn=respond,
|
@@ -34,4 +39,5 @@ iface = gr.Interface(
|
|
34 |
description="Light AI bot with Hindi + English voice support."
|
35 |
)
|
36 |
|
37 |
-
|
|
|
|
3 |
import gradio as gr
|
4 |
from gtts import gTTS
|
5 |
from langdetect import detect
|
6 |
+
import tempfile
|
7 |
|
8 |
model_name = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
|
9 |
|
|
|
14 |
if not user_input:
|
15 |
return "Please ask something.", None
|
16 |
|
17 |
+
# Detect language
|
18 |
detected_lang = detect(user_input)
|
19 |
|
20 |
+
# Prepare prompt
|
21 |
prompt = f"[INST] {user_input} [/INST]"
|
22 |
inputs = tokenizer(prompt, return_tensors="pt")
|
23 |
outputs = model.generate(**inputs, max_new_tokens=256, pad_token_id=tokenizer.eos_token_id)
|
24 |
|
25 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
26 |
|
27 |
+
# Save voice response to a temporary file (Hugging Face compatible)
|
28 |
tts = gTTS(text=response, lang='hi' if detected_lang == 'hi' else 'en')
|
29 |
+
temp_audio = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
|
30 |
+
tts.save(temp_audio.name)
|
31 |
+
|
32 |
+
return response, temp_audio.name
|
33 |
|
34 |
iface = gr.Interface(
|
35 |
fn=respond,
|
|
|
39 |
description="Light AI bot with Hindi + English voice support."
|
40 |
)
|
41 |
|
42 |
+
if __name__ == "__main__":
|
43 |
+
iface.launch()
|