import gradio as gr import ai_functions import os from openai import OpenAI import json def transform_meal_description_to_json(description): return ai_functions.ai_function("Meal2Json", description) def analyze_user_data(likes, dislikes, allergens, diet_plan, calorie_intake, meal_description_json): user_data = { "likes": likes, "dislikes": dislikes, "allergens": allergens, "diet_plan": diet_plan, "calorie_intake": calorie_intake } description = json.dumps({"meal_data": meal_description_json, "user_data": user_data}) return ai_functions.ai_function("AnylizeJson", description) def verify_api_key(api_key): if os.environ.get("PASSWORD") == api_key: ai_functions.client = OpenAI(api_key=os.environ.get("API_KEY")) else: ai_functions.client = OpenAI(api_key=api_key) try: ai_functions.client.models.list() return f"OpenAI API is verified." except Exception as e: return f"OpenAI API isn't verified." with gr.Blocks() as app: with gr.Tab("OpenAI API Settings"): with gr.Row(): api_key_input = gr.Textbox(label="OpenAI API Key") api_key_ioutput = gr.Textbox(label="Info",interactive=False) Verify_button = gr.Button("Verify") Verify_button.click( verify_api_key, inputs=[api_key_input], outputs=[api_key_ioutput] ) with gr.Tab("添加餐點描述"): with gr.Row(): meal_description_input = gr.Textbox(label="餐點描述", placeholder="請輸入餐點描述...") meal_description_json_output = gr.Textbox(label="餐點 Json", interactive=False) transform_button = gr.Button("轉換為 JSON 格式") transform_button.click( transform_meal_description_to_json, inputs=[meal_description_input], outputs=[meal_description_json_output] ) with gr.Tab("使用者資訊"): with gr.Column(): likes_input = gr.Textbox(label="喜歡的餐點") dislikes_input = gr.Textbox(label="不喜歡的餐點") allergens_checklist = gr.CheckboxGroup(label="選擇過敏原", choices=["海鮮", "麩質", "堅果", "乳糖"]) diet_plan_input = gr.Textbox(label="飲食計畫") calorie_intake_input = gr.Textbox(label="卡路里攝取標準") analyze_output = gr.Textbox(label="分析結果", interactive=False) analyze_button = gr.Button("分析並推薦餐點") analyze_button.click( analyze_user_data, inputs=[likes_input, dislikes_input, allergens_checklist, diet_plan_input, calorie_intake_input, meal_description_json_output], outputs=[analyze_output] ) app.launch()