import os import random import uuid import gradio as gr from pydub import AudioSegment import whisper import asyncio import platform # Load Whisper model whisper_model = whisper.load_model("tiny") # Supported languages lang_map = { "English": "en", "Hindi": "hi", "Telugu": "te", "Spanish": "es", "Tamil": "ta", "Gujarati": "gu", "Kannada": "kn", "Bengali": "bn", "Marathi": "mr", "Punjabi": "pa", "Urdu": "ur" } # Local food database local_foods = { "vegetables": [ {"name": "carrot", "calories": 41, "iron": 0.3, "fiber": 2.8, "vitamin_c": 6}, {"name": "spinach", "calories": 23, "iron": 2.7, "fiber": 2.2, "vitamin_c": 28}, {"name": "beetroot", "calories": 43, "iron": 0.8, "fiber": 2.0, "vitamin_c": 4} ], "grains": [ {"name": "rice", "calories": 130, "iron": 1.2, "fiber": 0.4, "protein": 2.7}, {"name": "wheat", "calories": 340, "iron": 3.2, "fiber": 12.2, "protein": 13}, {"name": "millet", "calories": 119, "iron": 3.0, "fiber": 8.5, "protein": 3.5} ], "proteins": [ {"name": "lentils", "calories": 116, "iron": 3.3, "fiber": 7.9, "protein": 9}, {"name": "chicken", "calories": 239, "iron": 1.3, "fiber": 0.0, "protein": 27}, {"name": "tofu", "calories": 76, "iron": 1.6, "fiber": 0.3, "protein": 8} ] } # Select random foods def precompute_foods(): return { "grain": random.choice(local_foods['grains']), "veg1": random.choice(local_foods['vegetables']), "veg2": random.choice(local_foods['vegetables']), "protein": random.choice(local_foods['proteins']) } # Output templates (shortened for brevity here, use your full dictionary) def get_output_template(lang): templates = { "English": """ 🌾 Personalized Diet Plan ──────────────────────────── 👤 Age: {age}, Gender: {gender} 💼 Occupation: {occupation} 🏃 Activity Level: {activity_level} ⚕️ Health Conditions: {health_conditions} 🍽️ Preferences: {dietary_preferences} ⚠️ Allergies: {allergies} 💰 Budget: ₹{budget}/day 🥣 Breakfast: {breakfast} 🍛 Lunch: {lunch} 🥗 Dinner: {dinner} 📊 Summary: Balanced meals with local ingredients. """ } return templates.get(lang, templates["English"]) # Transcription async def transcribe_audio(audio_path, lang): try: if not audio_path or not os.path.exists(audio_path): return "" temp_wav = f"temp_{uuid.uuid4().hex}.wav" audio = AudioSegment.from_file(audio_path) audio = audio.set_frame_rate(16000).set_channels(1) audio.export(temp_wav, format="wav") result = whisper_model.transcribe(temp_wav, language=lang_map.get(lang, "en"), fp16=False) os.remove(temp_wav) return result.get("text", "").strip() except Exception as e: return f"❌ Error: {str(e)}" # Full pipeline async def full_pipeline(health_audio, diet_audio, allergy_audio, health_text, diet_text, allergy_text, age, gender, weight, height, occupation, activity, budget, lang): health_conditions = health_text or await transcribe_audio(health_audio, lang) dietary_preferences = diet_text or await transcribe_audio(diet_audio, lang) allergies = allergy_text or await transcribe_audio(allergy_audio, lang) return generate_diet(age, gender, weight, height, occupation, activity, health_conditions, dietary_preferences, allergies, budget, lang) # Diet generator def generate_diet(age, gender, weight, height, occupation, activity_level, health_conditions, dietary_preferences, allergies, budget, lang): foods = precompute_foods() grain, veg1, veg2, protein = foods['grain'], foods['veg1'], foods['veg2'], foods['protein'] breakfast = f"{grain['name']} porridge with {veg1['name']}" lunch = f"{protein['name']} with {grain['name']} and {veg2['name']}" dinner = f"{veg1['name']} curry, {veg2['name']} salad, and {protein['name']}" nutrients = ["calories", "iron", "fiber", "protein", "vitamin_c"] nutrition = {n: round(grain.get(n, 0) * 2 + veg1.get(n, 0) + veg2.get(n, 0) * 2 + protein.get(n, 0) * 2, 1) for n in nutrients} plan = get_output_template(lang).format( age=age, gender=gender, occupation=occupation, activity_level=activity_level, health_conditions=health_conditions or "None", dietary_preferences=dietary_preferences or "None", allergies=allergies or "None", budget=budget, breakfast=breakfast, lunch=lunch, dinner=dinner ) nutrition_summary = f""" 🍽️ Nutritional Summary: ------------------------------ 🔥 Calories: {nutrition['calories']} kcal 🩸 Iron: {nutrition['iron']} mg 🧬 Protein: {nutrition['protein']} g 🌾 Fiber: {nutrition['fiber']} g 🍊 Vitamin C: {nutrition['vitamin_c']} mg """ return plan + nutrition_summary # UI def render_ui(default_lang="English"): with gr.Blocks() as demo: lang_sel = gr.Dropdown(label="Language", choices=list(lang_map.keys()), value=default_lang) age = gr.Number(label="Age", value=30, minimum=1) gender = gr.Radio(["Male", "Female", "Other"], label="Gender") weight = gr.Number(label="Weight (kg)", value=70, minimum=1) height = gr.Number(label="Height (cm)", value=170, minimum=1) occupation = gr.Textbox(label="Occupation") activity = gr.Radio(["Low", "Moderate", "High"], label="Activity Level") with gr.Row(): health_audio = gr.Audio(label="🎤 Speak Health Conditions", type="filepath") health_text = gr.Textbox(label="Or Type Health Conditions") with gr.Row(): diet_audio = gr.Audio(label="🎤 Speak Dietary Preferences", type="filepath") diet_text = gr.Textbox(label="Or Type Dietary Preferences") with gr.Row(): allergy_audio = gr.Audio(label="🎤 Speak Allergies", type="filepath") allergy_text = gr.Textbox(label="Or Type Allergies") budget = gr.Number(label="Daily Budget (₹)", value=200, minimum=0) output = gr.Textbox(label="Generated Diet Plan", lines=15) gr.Button("🎯 Generate Plan").click( full_pipeline, inputs=[health_audio, diet_audio, allergy_audio, health_text, diet_text, allergy_text, age, gender, weight, height, occupation, activity, budget, lang_sel], outputs=output ) return demo async def main(): demo = render_ui() await demo.launch(server_name="0.0.0.0", server_port=7860) if __name__ == "__main__": asyncio.run(main())