ghsambit commited on
Commit
b1fb452
·
1 Parent(s): 7e6b86d

Updated files to fix runtime error and add gTTS

Browse files
Files changed (2) hide show
  1. app.py +42 -84
  2. requirements.txt +4 -4
app.py CHANGED
@@ -1,84 +1,42 @@
1
- import gradio as gr
2
- import requests
3
- import os
4
- import speech_recognition as sr
5
- import pyttsx3
6
-
7
-
8
- TOGETHER_API_KEY = "92269fefcc8df100e2d395daab78ba62c0f3b64dbd477227020dce924e7821fe"
9
-
10
- # Text-to-speech setup
11
- tts_engine = pyttsx3.init()
12
-
13
- def speak(text):
14
- tts_engine.say(text)
15
- tts_engine.runAndWait()
16
-
17
- # 🎤 Speech-to-text function
18
- def transcribe_audio(audio):
19
- recognizer = sr.Recognizer()
20
- with sr.AudioFile(audio) as source:
21
- audio_data = recognizer.record(source)
22
- try:
23
- return recognizer.recognize_google(audio_data)
24
- except sr.UnknownValueError:
25
- return "Sorry, I could not understand your voice."
26
- except sr.RequestError:
27
- return "Could not request results from speech recognition service."
28
-
29
- # 🤖 Call Together API
30
- def call_together_api(message, history):
31
- messages = [{"role": "system", "content": "You are Sambit AI, a helpful assistant."}]
32
- for user_msg, ai_msg in history:
33
- messages.append({"role": "user", "content": user_msg})
34
- messages.append({"role": "assistant", "content": ai_msg})
35
- messages.append({"role": "user", "content": message})
36
-
37
- response = requests.post(
38
- "https://api.together.xyz/v1/chat/completions",
39
- headers={
40
- "Authorization": f"Bearer {TOGETHER_API_KEY}",
41
- "Content-Type": "application/json"
42
- },
43
- json={
44
- "model": "meta-llama/Llama-3-70b-instruct",
45
- "messages": messages,
46
- "temperature": 0.7,
47
- }
48
- )
49
-
50
- if response.status_code == 200:
51
- reply = response.json()["choices"][0]["message"]["content"]
52
- speak(reply)
53
- return reply
54
- elif response.status_code == 429:
55
- return "Rate limit reached. Please wait a bit."
56
- else:
57
- return f"Error: {response.json()['error']['message']}"
58
-
59
- # 🎤🎧 UI function (text + voice input)
60
- def chatbot_interface(message, audio, history=[]):
61
- if audio is not None:
62
- message = transcribe_audio(audio)
63
- if message.strip() == "":
64
- return "", history
65
- reply = call_together_api(message, history)
66
- history.append((message, reply))
67
- return "", history
68
-
69
- # 🎨 Dark theme UI
70
- with gr.Blocks(theme=gr.themes.Monochrome()) as demo:
71
- gr.Markdown("# 🤖 Sambit AI\nAsk anything via text or voice")
72
-
73
- chatbot = gr.Chatbot(type="messages")
74
- msg = gr.Textbox(placeholder="Type your message here...", label="Text Input")
75
- audio_input = gr.Audio(sources="microphone", type="filepath", label="Or speak here")
76
-
77
- submit_btn = gr.Button("Send")
78
-
79
- state = gr.State([])
80
-
81
- submit_btn.click(chatbot_interface, inputs=[msg, audio_input, state], outputs=[msg, state])
82
- msg.submit(chatbot_interface, inputs=[msg, audio_input, state], outputs=[msg, state])
83
-
84
- demo.launch()
 
1
+
2
+ import gradio as gr
3
+ from huggingface_hub import InferenceClient
4
+ from gtts import gTTS
5
+ import tempfile
6
+
7
+ client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
+
9
+ def speak_text(text):
10
+ tts = gTTS(text)
11
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as fp:
12
+ tts.save(fp.name)
13
+ return fp.name
14
+
15
+ def respond(message, history):
16
+ history = history or []
17
+ prompt = ""
18
+ for user_msg, bot_msg in history:
19
+ prompt += f"<|user|> {user_msg}\n<|assistant|> {bot_msg}\n"
20
+ prompt += f"<|user|> {message}\n<|assistant|>"
21
+
22
+ response = client.text_generation(prompt, max_new_tokens=512, temperature=0.7, top_p=0.95, repetition_penalty=1.0, do_sample=True)
23
+ response = response.strip().split("<|assistant|>")[-1].strip()
24
+
25
+ history.append((message, response))
26
+ audio_path = speak_text(response)
27
+ return history, history, audio_path
28
+
29
+ with gr.Blocks() as demo:
30
+ gr.Markdown("# Sambit AI 🤖")
31
+ chatbot = gr.Chatbot([], label="Chat with AI", type="messages")
32
+ msg = gr.Textbox(label="Type your message here")
33
+ audio_output = gr.Audio(label="AI Voice", interactive=False)
34
+
35
+ clear = gr.Button("Clear Chat")
36
+
37
+ state = gr.State([])
38
+
39
+ msg.submit(respond, [msg, state], [chatbot, state, audio_output])
40
+ clear.click(lambda: ([], [], None), None, [chatbot, state, audio_output])
41
+
42
+ demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
requirements.txt CHANGED
@@ -1,4 +1,4 @@
1
- gradio
2
- requests
3
- speechrecognition
4
- pyttsx3
 
1
+
2
+ gradio
3
+ huggingface_hub
4
+ gTTS