Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import whisper
|
| 3 |
+
from huggingface_hub import InferenceClient
|
| 4 |
+
from gtts import gTTS
|
| 5 |
+
import torch
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
# --- 1. YOUR COMPANY DATA (Strict Instructions added) ---
|
| 9 |
+
COMPANY_KNOWLEDGE = """
|
| 10 |
+
You are the reception AI for 'TechNova Solutions'.
|
| 11 |
+
Rules for answering:
|
| 12 |
+
1. Answer ONLY based on the facts below.
|
| 13 |
+
2. Keep answers EXTREMELY SHORT (Maximum 1 sentence).
|
| 14 |
+
3. Be direct. Do not say "Hello" or "Sure" every time. Just give the answer.
|
| 15 |
+
|
| 16 |
+
FACTS:
|
| 17 |
+
- WHAT WE DO: We build custom solar-powered drones for agriculture.
|
| 18 |
+
- PRICING: Basic drone is $500. Pro drone is $1200.
|
| 19 |
+
- RETURN POLICY: 30-day money-back guarantee.
|
| 20 |
+
- CONTACT: Email help@technova.com or call +1-555-0199.
|
| 21 |
+
- HOURS: Mon-Fri, 9 AM to 5 PM EST.
|
| 22 |
+
"""
|
| 23 |
+
|
| 24 |
+
# --- 2. SETUP ---
|
| 25 |
+
# Replace with your actual token
|
| 26 |
+
hf_token = os.getenv("HF_TOKEN") or "YOUR_HF_TOKEN_HERE"
|
| 27 |
+
client = InferenceClient(api_key=hf_token)
|
| 28 |
+
|
| 29 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 30 |
+
whisper_model = whisper.load_model("tiny", device=device)
|
| 31 |
+
|
| 32 |
+
def voice_chat(audio, history):
|
| 33 |
+
if audio is None:
|
| 34 |
+
return None, "", history
|
| 35 |
+
|
| 36 |
+
# A. Initialize History with Company Knowledge (if empty)
|
| 37 |
+
if not history:
|
| 38 |
+
history = [{"role": "system", "content": COMPANY_KNOWLEDGE}]
|
| 39 |
+
|
| 40 |
+
try:
|
| 41 |
+
# B. Hear
|
| 42 |
+
transcription = whisper_model.transcribe(audio, fp16=False)["text"]
|
| 43 |
+
|
| 44 |
+
# C. Think
|
| 45 |
+
# We append the user text
|
| 46 |
+
history.append({"role": "user", "content": transcription})
|
| 47 |
+
|
| 48 |
+
response = client.chat.completions.create(
|
| 49 |
+
model="HuggingFaceH4/zephyr-7b-beta",
|
| 50 |
+
messages=history,
|
| 51 |
+
max_tokens=60, # Reduced to 60 to force brevity
|
| 52 |
+
temperature=0.5 # Lower temperature = more precise/less creative
|
| 53 |
+
)
|
| 54 |
+
ai_text = response.choices[0].message.content
|
| 55 |
+
|
| 56 |
+
history.append({"role": "assistant", "content": ai_text})
|
| 57 |
+
|
| 58 |
+
# D. Speak
|
| 59 |
+
tts = gTTS(text=ai_text, lang='en')
|
| 60 |
+
audio_path = "response.mp3"
|
| 61 |
+
tts.save(audio_path)
|
| 62 |
+
|
| 63 |
+
return audio_path, ai_text, history
|
| 64 |
+
|
| 65 |
+
except Exception as e:
|
| 66 |
+
return None, f"Error: {str(e)}", history
|
| 67 |
+
|
| 68 |
+
# --- 3. INTERFACE ---
|
| 69 |
+
with gr.Blocks(theme="soft") as demo:
|
| 70 |
+
gr.Markdown("## 🏢 Quick-Response Company AI")
|
| 71 |
+
|
| 72 |
+
conversation_history = gr.State([])
|
| 73 |
+
|
| 74 |
+
with gr.Row():
|
| 75 |
+
input_audio = gr.Audio(sources=["microphone"], type="filepath", label="Ask Question")
|
| 76 |
+
|
| 77 |
+
with gr.Row():
|
| 78 |
+
output_audio = gr.Audio(label="AI Response", autoplay=True)
|
| 79 |
+
output_text = gr.Textbox(label="Transcript")
|
| 80 |
+
|
| 81 |
+
clear_btn = gr.Button("Reset")
|
| 82 |
+
|
| 83 |
+
input_audio.change(
|
| 84 |
+
voice_chat,
|
| 85 |
+
inputs=[input_audio, conversation_history],
|
| 86 |
+
outputs=[output_audio, output_text, conversation_history]
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
+
clear_btn.click(lambda: ([], None, ""), outputs=[conversation_history, output_audio, output_text])
|
| 90 |
+
|
| 91 |
+
demo.launch(debug=True)
|