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

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +84 -64
  2. requirements.txt +4 -1
app.py CHANGED
@@ -1,64 +1,84 @@
1
- import gradio as gr
2
- from huggingface_hub import InferenceClient
3
-
4
- """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
- """
7
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
-
9
-
10
- def respond(
11
- message,
12
- history: list[tuple[str, str]],
13
- system_message,
14
- max_tokens,
15
- temperature,
16
- top_p,
17
- ):
18
- messages = [{"role": "system", "content": system_message}]
19
-
20
- for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
25
-
26
- messages.append({"role": "user", "content": message})
27
-
28
- response = ""
29
-
30
- for message in client.chat_completion(
31
- messages,
32
- max_tokens=max_tokens,
33
- stream=True,
34
- temperature=temperature,
35
- top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
38
-
39
- response += token
40
- yield response
41
-
42
-
43
- """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
- """
46
- demo = gr.ChatInterface(
47
- respond,
48
- additional_inputs=[
49
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
50
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
51
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
52
- gr.Slider(
53
- minimum=0.1,
54
- maximum=1.0,
55
- value=0.95,
56
- step=0.05,
57
- label="Top-p (nucleus sampling)",
58
- ),
59
- ],
60
- )
61
-
62
-
63
- if __name__ == "__main__":
64
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()
requirements.txt CHANGED
@@ -1 +1,4 @@
1
- huggingface_hub==0.25.2
 
 
 
 
1
+ gradio
2
+ requests
3
+ speechrecognition
4
+ pyttsx3