Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,64 +1,95 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
)
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
"""
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 os
|
2 |
import gradio as gr
|
3 |
+
import requests
|
4 |
+
from sentence_transformers import SentenceTransformer, util
|
5 |
+
import torch
|
6 |
+
import json
|
7 |
+
import urllib.parse
|
8 |
+
|
9 |
+
# Fetch Hugging Face API Token securely from environment variables
|
10 |
+
HF_API_TOKEN = os.getenv("HF_API_TOKEN") # This fetches the token securely
|
11 |
+
|
12 |
+
WHISPER_API_URL = "https://api-inference.huggingface.co/models/openai/whisper-large"
|
13 |
+
LLAMA_API_URL = "https://api-inference.huggingface.co/models/meta-llama/Llama-2-7b-chat-hf"
|
14 |
+
|
15 |
+
# Load SentenceTransformer model for retrieval
|
16 |
+
retriever_model = SentenceTransformer("distiluse-base-multilingual-cased-v2")
|
17 |
+
|
18 |
+
# Load dataset
|
19 |
+
with open("qa_dataset.json", "r", encoding="utf-8") as f:
|
20 |
+
qa_data = json.load(f)
|
21 |
+
|
22 |
+
# Function to transcribe audio using Whisper
|
23 |
+
def transcribe_audio(audio_file):
|
24 |
+
headers = {"Authorization": f"Bearer {HF_API_TOKEN}"}
|
25 |
+
with open(audio_file, "rb") as f:
|
26 |
+
response = requests.post(WHISPER_API_URL, headers=headers, data=f)
|
27 |
+
return response.json()["text"]
|
28 |
+
|
29 |
+
# Function to generate TTS audio URL (Google Translate API for Tamil Voice)
|
30 |
+
def get_tts_audio_url(text, lang="ta"):
|
31 |
+
safe_text = text.replace(" ", "+")
|
32 |
+
return f"https://translate.google.com/translate_tts?ie=UTF-8&q={safe_text}&tl={lang}&client=tw-ob"
|
33 |
+
|
34 |
+
# Function to retrieve a relevant response from the Q&A dataset using SentenceTransformer
|
35 |
+
def get_bot_response(query):
|
36 |
+
query_embedding = retriever_model.encode(query, convert_to_tensor=True)
|
37 |
+
qa_embeddings = retriever_model.encode([qa["question"] for qa in qa_data], convert_to_tensor=True)
|
38 |
+
|
39 |
+
scores = util.pytorch_cos_sim(query_embedding, qa_embeddings)
|
40 |
+
best_idx = torch.argmax(scores)
|
41 |
+
|
42 |
+
top_qa = qa_data[best_idx]
|
43 |
+
prompt = f"User asked: {query}\nRelevant FAQ: {top_qa['question']}\nAnswer: {top_qa['answer']}\nNow generate a helpful and fluent Tamil response to the user query."
|
44 |
+
|
45 |
+
# Use LLaMA for generating the refined response
|
46 |
+
headers = {"Authorization": f"Bearer {HF_API_TOKEN}"}
|
47 |
+
payload = {
|
48 |
+
"inputs": prompt,
|
49 |
+
"parameters": {"temperature": 0.7, "max_new_tokens": 150, "return_full_text": False},
|
50 |
+
}
|
51 |
+
response = requests.post(LLAMA_API_URL, headers=headers, json=payload)
|
52 |
+
result = response.json()
|
53 |
+
|
54 |
+
if isinstance(result, list) and "generated_text" in result[0]:
|
55 |
+
return result[0]["generated_text"]
|
56 |
+
else:
|
57 |
+
return "மன்னிக்கவும், நான் இந்த கேள்விக்கு பதில் தர முடியவில்லை."
|
58 |
+
|
59 |
+
# Gradio interface function
|
60 |
+
def chatbot(audio, message, history, system_message, max_tokens, temperature, top_p):
|
61 |
+
if audio is not None:
|
62 |
+
# Save the audio file temporarily
|
63 |
+
with open("temp.wav", "wb") as f:
|
64 |
+
f.write(audio.read())
|
65 |
+
|
66 |
+
# Transcribe the audio using Whisper
|
67 |
+
transcript = transcribe_audio("temp.wav")
|
68 |
+
message = transcript # Use the transcript as the input message
|
69 |
+
|
70 |
+
# Get the bot response using the text input or transcribed audio
|
71 |
+
response = get_bot_response(message)
|
72 |
+
|
73 |
+
# Generate the TTS audio URL
|
74 |
+
audio_url = get_tts_audio_url(response)
|
75 |
+
|
76 |
+
return response, audio_url
|
77 |
+
|
78 |
+
# Define Gradio interface
|
79 |
+
demo = gr.Interface(
|
80 |
+
fn=chatbot,
|
81 |
+
inputs=[
|
82 |
+
gr.Audio(source="microphone", type="file", label="Speak to the Bot"),
|
83 |
+
gr.Textbox(value="How can I help you?", label="Text Input (optional)"),
|
84 |
+
gr.State(),
|
85 |
+
gr.Textbox(value="You are a friendly chatbot.", label="System message"),
|
86 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
87 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
88 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
|
|
|
|
|
|
|
|
|
|
|
|
|
89 |
],
|
90 |
+
outputs=[gr.Textbox(label="Response"), gr.Audio(label="Bot's Voice Response (Tamil)")],
|
91 |
+
live=True,
|
92 |
)
|
93 |
|
|
|
94 |
if __name__ == "__main__":
|
95 |
demo.launch()
|