Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,15 +2,18 @@ import gradio as gr
|
|
| 2 |
from gtts import gTTS
|
| 3 |
import os
|
| 4 |
import uuid
|
|
|
|
| 5 |
from transformers import pipeline
|
| 6 |
from deep_translator import GoogleTranslator
|
| 7 |
-
import
|
| 8 |
-
from langdetect import detect
|
| 9 |
|
| 10 |
-
# Load
|
|
|
|
|
|
|
|
|
|
| 11 |
qa_pipeline = pipeline("text2text-generation", model="google/flan-t5-base")
|
| 12 |
|
| 13 |
-
#
|
| 14 |
knowledge_base = {
|
| 15 |
"best soil for gardening": "Loamy soil is best for gardening because it retains moisture and nutrients but also drains well.",
|
| 16 |
"best soil for tomato": "Tomatoes grow best in well-drained loamy soil that is rich in organic matter.",
|
|
@@ -19,54 +22,53 @@ knowledge_base = {
|
|
| 19 |
"what soil for roses": "Roses grow well in loamy, well-drained soil with a pH between 6.0 and 7.0."
|
| 20 |
}
|
| 21 |
|
|
|
|
| 22 |
def detect_language(text):
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
except:
|
| 26 |
-
return "en"
|
| 27 |
|
| 28 |
def generate_response(text):
|
| 29 |
-
# Detect language and translate
|
| 30 |
original_lang = detect_language(text)
|
| 31 |
translated_input = GoogleTranslator(source='auto', target='en').translate(text)
|
| 32 |
|
| 33 |
-
# Use knowledge base
|
| 34 |
response_text = ""
|
| 35 |
for key in knowledge_base:
|
| 36 |
if key in translated_input.lower():
|
| 37 |
response_text = knowledge_base[key]
|
| 38 |
break
|
| 39 |
|
| 40 |
-
# Otherwise
|
| 41 |
if not response_text:
|
| 42 |
response_text = qa_pipeline(translated_input, max_length=100)[0]['generated_text']
|
| 43 |
|
| 44 |
-
# Translate
|
| 45 |
translated_response = GoogleTranslator(source='en', target=original_lang).translate(response_text)
|
| 46 |
|
| 47 |
-
# Text
|
| 48 |
try:
|
| 49 |
tts = gTTS(text=translated_response, lang=original_lang)
|
| 50 |
-
except
|
| 51 |
-
tts = gTTS(text=translated_response, lang='en')
|
| 52 |
-
|
| 53 |
-
audio_path = os.path.join(tempfile.gettempdir(),
|
| 54 |
tts.save(audio_path)
|
| 55 |
|
| 56 |
return translated_response, audio_path
|
| 57 |
|
| 58 |
-
# Gradio
|
| 59 |
with gr.Blocks() as demo:
|
| 60 |
-
gr.Markdown("π **Multilingual Voice Chatbot**\nAsk
|
| 61 |
|
| 62 |
with gr.Row():
|
| 63 |
with gr.Column():
|
| 64 |
-
text_input = gr.Textbox(label="
|
| 65 |
-
ask_button = gr.Button("
|
| 66 |
|
| 67 |
with gr.Column():
|
| 68 |
-
text_output = gr.Textbox(label="π
|
| 69 |
-
audio_output = gr.Audio(label="
|
| 70 |
|
| 71 |
ask_button.click(fn=generate_response, inputs=text_input, outputs=[text_output, audio_output])
|
| 72 |
|
|
|
|
| 2 |
from gtts import gTTS
|
| 3 |
import os
|
| 4 |
import uuid
|
| 5 |
+
import tempfile
|
| 6 |
from transformers import pipeline
|
| 7 |
from deep_translator import GoogleTranslator
|
| 8 |
+
import fasttext
|
|
|
|
| 9 |
|
| 10 |
+
# Load language detection model
|
| 11 |
+
lang_model = fasttext.load_model("lid.176.bin")
|
| 12 |
+
|
| 13 |
+
# Load Hugging Face model
|
| 14 |
qa_pipeline = pipeline("text2text-generation", model="google/flan-t5-base")
|
| 15 |
|
| 16 |
+
# Plant/Soil knowledge base
|
| 17 |
knowledge_base = {
|
| 18 |
"best soil for gardening": "Loamy soil is best for gardening because it retains moisture and nutrients but also drains well.",
|
| 19 |
"best soil for tomato": "Tomatoes grow best in well-drained loamy soil that is rich in organic matter.",
|
|
|
|
| 22 |
"what soil for roses": "Roses grow well in loamy, well-drained soil with a pH between 6.0 and 7.0."
|
| 23 |
}
|
| 24 |
|
| 25 |
+
# FastText-based language detection
|
| 26 |
def detect_language(text):
|
| 27 |
+
lang = lang_model.predict(text)[0][0].replace("__label__", "")
|
| 28 |
+
return lang
|
|
|
|
|
|
|
| 29 |
|
| 30 |
def generate_response(text):
|
| 31 |
+
# Detect language and translate
|
| 32 |
original_lang = detect_language(text)
|
| 33 |
translated_input = GoogleTranslator(source='auto', target='en').translate(text)
|
| 34 |
|
| 35 |
+
# Use knowledge base if match
|
| 36 |
response_text = ""
|
| 37 |
for key in knowledge_base:
|
| 38 |
if key in translated_input.lower():
|
| 39 |
response_text = knowledge_base[key]
|
| 40 |
break
|
| 41 |
|
| 42 |
+
# Otherwise use Hugging Face model
|
| 43 |
if not response_text:
|
| 44 |
response_text = qa_pipeline(translated_input, max_length=100)[0]['generated_text']
|
| 45 |
|
| 46 |
+
# Translate back to original language
|
| 47 |
translated_response = GoogleTranslator(source='en', target=original_lang).translate(response_text)
|
| 48 |
|
| 49 |
+
# Text-to-speech
|
| 50 |
try:
|
| 51 |
tts = gTTS(text=translated_response, lang=original_lang)
|
| 52 |
+
except:
|
| 53 |
+
tts = gTTS(text=translated_response, lang='en')
|
| 54 |
+
|
| 55 |
+
audio_path = os.path.join(tempfile.gettempdir(), f"{uuid.uuid4()}.mp3")
|
| 56 |
tts.save(audio_path)
|
| 57 |
|
| 58 |
return translated_response, audio_path
|
| 59 |
|
| 60 |
+
# Gradio UI
|
| 61 |
with gr.Blocks() as demo:
|
| 62 |
+
gr.Markdown("π **Multilingual Voice Chatbot**\nAsk in any language about plants/soil!")
|
| 63 |
|
| 64 |
with gr.Row():
|
| 65 |
with gr.Column():
|
| 66 |
+
text_input = gr.Textbox(label="π± Ask a Question")
|
| 67 |
+
ask_button = gr.Button("π§ Get Answer")
|
| 68 |
|
| 69 |
with gr.Column():
|
| 70 |
+
text_output = gr.Textbox(label="π Response")
|
| 71 |
+
audio_output = gr.Audio(label="π Voice", autoplay=True)
|
| 72 |
|
| 73 |
ask_button.click(fn=generate_response, inputs=text_input, outputs=[text_output, audio_output])
|
| 74 |
|