Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import speech_recognition as sr
|
| 3 |
+
import pyttsx3
|
| 4 |
+
import requests
|
| 5 |
+
|
| 6 |
+
# Setup TTS
|
| 7 |
+
engine = pyttsx3.init()
|
| 8 |
+
voices = engine.getProperty('voices')
|
| 9 |
+
# Try selecting a male voice
|
| 10 |
+
for v in voices:
|
| 11 |
+
if "male" in v.name.lower() or "david" in v.name.lower():
|
| 12 |
+
engine.setProperty('voice', v.id)
|
| 13 |
+
break
|
| 14 |
+
engine.setProperty('rate', 150)
|
| 15 |
+
|
| 16 |
+
# Mistral API Config
|
| 17 |
+
API_KEY = "qUpn0MpfhwXIMGl2AdCN4CV9L2fIfWc4"
|
| 18 |
+
MODEL_NAME = "mistral-small-latest"
|
| 19 |
+
API_URL = "https://api.mistral.ai/v1/chat/completions"
|
| 20 |
+
HEADERS = {
|
| 21 |
+
"Authorization": f"Bearer {API_KEY}",
|
| 22 |
+
"Content-Type": "application/json"
|
| 23 |
+
}
|
| 24 |
+
PERSONA = """You are Pavan Teja, a 24-year-old male AI/ML specialist from Vizag.
|
| 25 |
+
Always respond in first person as Pavan. When someone asks who you are,
|
| 26 |
+
introduce yourself as Pavan Teja, never mention being an AI model."""
|
| 27 |
+
|
| 28 |
+
conversation = [{"role": "system", "content": PERSONA}]
|
| 29 |
+
|
| 30 |
+
def recognize_and_respond(audio):
|
| 31 |
+
global conversation
|
| 32 |
+
recognizer = sr.Recognizer()
|
| 33 |
+
try:
|
| 34 |
+
with sr.AudioFile(audio) as source:
|
| 35 |
+
recorded = recognizer.record(source)
|
| 36 |
+
question = recognizer.recognize_google(recorded)
|
| 37 |
+
except Exception as e:
|
| 38 |
+
return f"Voice recognition error: {str(e)}"
|
| 39 |
+
|
| 40 |
+
conversation.append({"role": "user", "content": question})
|
| 41 |
+
|
| 42 |
+
try:
|
| 43 |
+
response = requests.post(
|
| 44 |
+
API_URL,
|
| 45 |
+
headers=HEADERS,
|
| 46 |
+
json={"model": MODEL_NAME, "messages": conversation[-8:]}
|
| 47 |
+
)
|
| 48 |
+
response.raise_for_status()
|
| 49 |
+
reply = response.json()["choices"][0]["message"]["content"]
|
| 50 |
+
conversation.append({"role": "assistant", "content": reply})
|
| 51 |
+
except Exception as e:
|
| 52 |
+
return f"API error: {str(e)}"
|
| 53 |
+
|
| 54 |
+
# Speak out
|
| 55 |
+
engine.say(reply)
|
| 56 |
+
engine.runAndWait()
|
| 57 |
+
|
| 58 |
+
return reply
|
| 59 |
+
|
| 60 |
+
demo = gr.Interface(
|
| 61 |
+
fn=recognize_and_respond,
|
| 62 |
+
inputs=gr.Audio(source="microphone", type="filepath", label="🎤 Speak Now"),
|
| 63 |
+
outputs=gr.Textbox(label="🧠 Pavan Says"),
|
| 64 |
+
title="🎙️ AI VoiceBot - Pavan Teja",
|
| 65 |
+
description="Talk to Pavan Teja, your AI/ML Specialist Assistant. Powered by Mistral AI + Offline TTS."
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
if __name__ == "__main__":
|
| 69 |
+
demo.launch()
|