ParthBhuptani commited on
Commit
0a794f0
·
verified ·
1 Parent(s): a00706b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -73
app.py CHANGED
@@ -1,18 +1,17 @@
1
  import gradio as gr
2
  import requests
3
- import json
4
- import datetime
5
 
6
- # Load credentials from secrets
7
- import os
8
 
9
- NEBIUS_API_KEY = os.getenv("NEBIUS_API_KEY")
10
- FOLDER_ID = os.getenv("FOLDER_ID")
 
 
 
 
 
11
 
12
- headers = {
13
- "Authorization": f"Bearer {NEBIUS_API_KEY.strip()}",
14
- "Content-Type": "application/json"
15
- }
16
  body = {
17
  "folderId": FOLDER_ID,
18
  "texts": [input_text],
@@ -20,75 +19,36 @@ headers = {
20
  "temperature": 0.6,
21
  "maxTokens": "2000"
22
  }
23
- res = requests.post("https://llm.api.cloud.yandex.net/foundationModels/v1/completion", headers=headers, json=body)
24
- if res.status_code == 200:
25
- return res.json()['result']['alternatives'][0]['message']['text']
26
- else:
27
- return f"Error: {res.status_code} - {res.text}"
28
-
29
- # === Recipe Planner Function ===
30
- def generate_recipe(diet, pantry):
31
- prompt = f"You are a smart kitchen assistant. Suggest a detailed recipe for a {diet} user with ingredients from the pantry: {pantry}. Include cooking steps and nutritional info."
32
- return ask_nebius(prompt)
33
-
34
- # === Weekly Meal Planner Function ===
35
- def weekly_meal_plan(diet):
36
- prompt = f"Generate a full 7-day meal plan (Breakfast, Lunch, Dinner) for a {diet} diet. Include variety and healthy options."
37
- return ask_nebius(prompt)
38
-
39
- # === PDF Download ===
40
- def download_text_as_pdf(text):
41
- from fpdf import FPDF
42
- pdf = FPDF()
43
- pdf.add_page()
44
- pdf.set_font("Arial", size=12)
45
- for line in text.split('\n'):
46
- pdf.cell(200, 10, txt=line, ln=True)
47
- filename = f"meal_plan_{datetime.datetime.now().strftime('%Y%m%d%H%M%S')}.pdf"
48
- pdf.output(filename)
49
- return filename
50
 
51
- # === Gradio Interface ===
52
- with gr.Blocks(theme=gr.themes.Base(), title="AgentChef") as demo:
53
- gr.Markdown("""
54
- ## 🍳 AgentChef: AI Recipe Planner & Smart Kitchen Assistant
55
-
56
- Plan meals, check pantry items, generate weekly diet charts, and download PDFs — all with AI!
57
- """)
 
 
 
58
 
59
- with gr.Row():
60
- diet_input = gr.Dropdown(label="🍽️ Dietary Preferences", choices=["Vegetarian", "Vegan", "Keto", "Paleo", "Regular"], value="Vegetarian")
61
- pantry_input = gr.Textbox(label="🌾 Pantry Items (comma-separated)", placeholder="e.g. tomato, rice, beans")
62
-
63
- with gr.Row():
64
- mic = gr.Audio(type="filepath", label="🎤 Speak Ingredients (Optional)")
65
- transcript_output = gr.Textbox(label="Speech-to-text Result")
66
 
67
  with gr.Row():
68
- generate_button = gr.Button("🍽 Generate Recipe")
69
- meal_button = gr.Button("🍒 Weekly Meal Plan")
70
-
71
- result = gr.Textbox(label="🌱 Output")
72
- pdf_button = gr.Button("📄 Download as PDF")
73
- file_output = gr.File(label="Download Link")
74
-
75
- # === Callbacks ===
76
- def transcribe_audio(audio_file):
77
- import speech_recognition as sr
78
- recognizer = sr.Recognizer()
79
- with sr.AudioFile(audio_file) as source:
80
- audio_data = recognizer.record(source)
81
- return recognizer.recognize_google(audio_data)
82
 
83
- mic.change(transcribe_audio, inputs=mic, outputs=transcript_output)
84
 
85
- def handle_generate(diet, pantry, spoken):
86
- pantry_all = (spoken or '') + ", " + (pantry or '')
87
- return generate_recipe(diet, pantry_all)
88
 
89
- generate_button.click(handle_generate, inputs=[diet_input, pantry_input, transcript_output], outputs=result)
90
- meal_button.click(weekly_meal_plan, inputs=diet_input, outputs=result)
91
- pdf_button.click(download_text_as_pdf, inputs=result, outputs=file_output)
92
 
93
- # === Launch ===
94
  demo.launch()
 
1
  import gradio as gr
2
  import requests
 
 
3
 
4
+ NEBIUS_API_KEY = "your_nebius_api_key"
5
+ FOLDER_ID = "your_folder_id"
6
 
7
+ def generate_recipe(dietary_pref, ingredients):
8
+ input_text = f"Create a recipe using these ingredients: {ingredients}. It should be {dietary_pref}."
9
+
10
+ headers = {
11
+ "Authorization": f"Api-Key {NEBIUS_API_KEY}",
12
+ "Content-Type": "application/json"
13
+ }
14
 
 
 
 
 
15
  body = {
16
  "folderId": FOLDER_ID,
17
  "texts": [input_text],
 
19
  "temperature": 0.6,
20
  "maxTokens": "2000"
21
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
+ try:
24
+ response = requests.post(
25
+ "https://llm.api.cloud.yandex.net/foundationModels/v1/textCompletion",
26
+ headers=headers,
27
+ json=body
28
+ )
29
+ result = response.json()
30
+ return result["result"]["alternatives"][0]["text"]
31
+ except Exception as e:
32
+ return f"❌ Error: {e}"
33
 
34
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
35
+ gr.Markdown("🍳 **AgentChef: AI Recipe Planner & Smart Kitchen Assistant**")
 
 
 
 
 
36
 
37
  with gr.Row():
38
+ dietary = gr.Dropdown(
39
+ label="Dietary Preferences",
40
+ choices=["Vegetarian", "Non-Vegetarian", "Vegan", "Keto", "Other"],
41
+ value="Vegetarian"
42
+ )
43
+ ingredients = gr.Textbox(
44
+ label="Ingredients Available",
45
+ placeholder="e.g., tomato, onion, garlic, paneer..."
46
+ )
 
 
 
 
 
47
 
48
+ submit = gr.Button("🍽️ Generate Recipe")
49
 
50
+ recipe_output = gr.Textbox(label="AI-Generated Recipe", lines=10)
 
 
51
 
52
+ submit.click(fn=generate_recipe, inputs=[dietary, ingredients], outputs=recipe_output)
 
 
53
 
 
54
  demo.launch()