Spaces:
Sleeping
Sleeping
| # app.py | |
| import gradio as gr | |
| import spaces | |
| from chatbot_logic import get_bot_response | |
| from knowledge_base import load_and_chunk_pdfs, create_vectorstore | |
| import os | |
| # if not os.path.exists("chroma/index"): | |
| # print("Vectorstore missing or empty β running ingestion.") | |
| # chunks = load_and_chunk_pdfs("meal_plans") | |
| # create_vectorstore(chunks) | |
| # === User Preference State === | |
| user_preferences = { | |
| "diet": None, | |
| "goal": None, | |
| "allergies": None, | |
| } | |
| # === GPU Eligibility Decorator === | |
| def dummy_gpu_task(): | |
| return "Triggered GPU on startup." | |
| def set_preferences(diet, goal, allergies): | |
| user_preferences["diet"] = diet | |
| user_preferences["goal"] = goal | |
| user_preferences["allergies"] = allergies | |
| return "Preferences updated. You can start chatting now!" | |
| def chat_interface(user_input, history): | |
| try: | |
| response = get_bot_response(user_input) | |
| return response | |
| except Exception as e: | |
| return f"β Failed to process: {str(e)}" | |
| with gr.Blocks(title="AI Meal Plan Assistant") as demo: | |
| gr.Markdown(""" | |
| # π½οΈ Smart Meal Plan Chatbot | |
| Ask me anything about meal plans, nutrition, or recipes from the PDFs! | |
| """) | |
| with gr.Accordion("Set Your Preferences (Optional)", open=True): | |
| with gr.Row(): | |
| diet = gr.Radio(label="Diet Type", choices=["None", "Vegan", "Vegetarian", "Keto", "Low-Carb"], value="None") | |
| goal = gr.Radio(label="Goal", choices=["None", "Weight Loss", "Muscle Gain", "Diabetic Friendly"], value="None") | |
| allergies = gr.Textbox(label="Allergies (comma-separated)", placeholder="e.g., peanuts, gluten") | |
| update_btn = gr.Button("Update Preferences") | |
| output = gr.Textbox(label="Status") | |
| update_btn.click(fn=set_preferences, inputs=[diet, goal, allergies], outputs=output) | |
| chatbot = gr.ChatInterface(chat_interface, title="Meal Planner Chatbot") | |
| if __name__ == "__main__": | |
| demo.launch() | |