Spaces:
Build error
Build error
| import gradio as gr | |
| from gtts import gTTS | |
| import os | |
| import uuid | |
| import tempfile | |
| from transformers import pipeline | |
| from deep_translator import GoogleTranslator | |
| import fasttext | |
| # Load language detection model | |
| lang_model = fasttext.load_model("lid.176.bin") | |
| # Load Hugging Face model | |
| qa_pipeline = pipeline("text2text-generation", model="google/flan-t5-base") | |
| # Plant/Soil knowledge base | |
| knowledge_base = { | |
| "best soil for gardening": "Loamy soil is best for gardening because it retains moisture and nutrients but also drains well.", | |
| "best soil for tomato": "Tomatoes grow best in well-drained loamy soil that is rich in organic matter.", | |
| "how to improve soil fertility": "You can improve soil fertility by adding compost, green manure, and crop rotation.", | |
| "ideal soil for strawberry": "Strawberries prefer slightly acidic, well-drained loamy soil with high organic content.", | |
| "what soil for roses": "Roses grow well in loamy, well-drained soil with a pH between 6.0 and 7.0." | |
| } | |
| # FastText-based language detection | |
| def detect_language(text): | |
| lang = lang_model.predict(text)[0][0].replace("__label__", "") | |
| return lang | |
| def generate_response(text): | |
| # Detect language and translate | |
| original_lang = detect_language(text) | |
| translated_input = GoogleTranslator(source='auto', target='en').translate(text) | |
| # Use knowledge base if match | |
| response_text = "" | |
| for key in knowledge_base: | |
| if key in translated_input.lower(): | |
| response_text = knowledge_base[key] | |
| break | |
| # Otherwise use Hugging Face model | |
| if not response_text: | |
| response_text = qa_pipeline(translated_input, max_length=100)[0]['generated_text'] | |
| # Translate back to original language | |
| translated_response = GoogleTranslator(source='en', target=original_lang).translate(response_text) | |
| # Text-to-speech | |
| try: | |
| tts = gTTS(text=translated_response, lang=original_lang) | |
| except: | |
| tts = gTTS(text=translated_response, lang='en') | |
| audio_path = os.path.join(tempfile.gettempdir(), f"{uuid.uuid4()}.mp3") | |
| tts.save(audio_path) | |
| return translated_response, audio_path | |
| # Gradio UI | |
| with gr.Blocks() as demo: | |
| gr.Markdown("π **Multilingual Voice Chatbot**\nAsk in any language about plants/soil!") | |
| with gr.Row(): | |
| with gr.Column(): | |
| text_input = gr.Textbox(label="π± Ask a Question") | |
| ask_button = gr.Button("π§ Get Answer") | |
| with gr.Column(): | |
| text_output = gr.Textbox(label="π Response") | |
| audio_output = gr.Audio(label="π Voice", autoplay=True) | |
| ask_button.click(fn=generate_response, inputs=text_input, outputs=[text_output, audio_output]) | |
| demo.launch(share=True) | |