evaliisah commited on
Commit
90793af
·
verified ·
1 Parent(s): 6b0bbcb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +85 -32
app.py CHANGED
@@ -1,50 +1,103 @@
1
 
2
 
3
  import gradio as gr
 
4
  from transformers import pipeline
5
- import torch
6
 
7
- # Initialize the text generation pipeline
8
- generator = pipeline('text-generation', model='gpt2')
 
9
 
10
- def generate_recipe(ingredient):
11
- """
12
- Generate a recipe based on the input ingredient/word
13
- """
14
- # Create a prompt template
15
- prompt = f"Recipe for {ingredient}:\n\nIngredients:\n"
 
 
 
 
 
 
 
 
 
 
16
 
17
- # Generate the recipe
18
- try:
19
- result = generator(
20
- prompt,
21
- max_length=300,
22
- num_return_sequences=1,
23
- temperature=0.7,
24
- pad_token_id=generator.tokenizer.eos_token_id
25
- )
26
 
27
- # Extract and format the generated recipe
28
- recipe = result[0]['generated_text']
29
 
30
- # Clean up the output
31
- recipe = recipe.replace(prompt, '')
32
 
33
- return recipe
34
-
35
- except Exception as e:
36
- return f"Error generating recipe: {str(e)}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
 
38
  # Create the Gradio interface
39
  iface = gr.Interface(
40
- fn=generate_recipe,
41
- inputs=gr.Textbox(lines=1, placeholder="Enter an ingredient or dish name..."),
42
- outputs=gr.Textbox(lines=10, label="Generated Recipe"),
43
- title="Recipe Generator",
44
- description="Enter an ingredient or dish name, and I'll generate a recipe for you!",
45
- examples=[["chocolate cake"], ["chicken curry"], ["pasta"]]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
  )
47
 
48
  # Launch the interface
49
  if __name__ == "__main__":
50
  iface.launch()
 
 
1
 
2
 
3
  import gradio as gr
4
+ import requests
5
  from transformers import pipeline
 
6
 
7
+ # Initialize translation models
8
+ translate_to_english = pipeline("translation", model="Helsinki-NLP/opus-mt-et-en")
9
+ translate_to_estonian = pipeline("translation", model="Helsinki-NLP/opus-mt-en-et")
10
 
11
+ # Spoonacular API settings
12
+ API_KEY = '0bf3de98d2654cb28f139df9fedb7db2'
13
+ SPOONACULAR_URL = "https://api.spoonacular.com/recipes/findByIngredients"
14
+
15
+ def translate_text(input_text, direction='et-en'):
16
+ """Translate text between Estonian and English."""
17
+ if direction == 'et-en':
18
+ result = translate_to_english(input_text)
19
+ else:
20
+ result = translate_to_estonian(input_text)
21
+ return result[0]['translation_text']
22
+
23
+ def find_recipes(ingredient, calories, wheat_content):
24
+ """Find recipes based on the ingredient and dietary preferences."""
25
+ # Translate ingredient to English
26
+ ingredient_en = translate_text(ingredient, direction='et-en')
27
 
28
+ params = {
29
+ "ingredients": ingredient_en,
30
+ "apiKey": API_KEY
31
+ }
32
+
33
+ response = requests.get(SPOONACULAR_URL, params=params)
34
+
35
+ if response.status_code == 200:
36
+ recipes = response.json()
37
 
38
+ if not recipes:
39
+ return "Ühtegi retsepti ei leitud.", ""
40
 
41
+ # Select the first recipe for demonstration
42
+ recipe = recipes[0]
43
 
44
+ # Prepare recipe details
45
+ recipe_name_en = recipe['title']
46
+ recipe_name_et = translate_text(recipe_name_en, direction='en-et')
47
+ recipe_link = f"https://spoonacular.com/recipes/{recipe['id']}-{recipe_name_en.replace(' ', '-')}"
48
+
49
+ # Translate ingredients to Estonian
50
+ missed_ingredients_en = ', '.join([ingredient['name'] for ingredient in recipe['missedIngredients']])
51
+ missed_ingredients_et = translate_text(missed_ingredients_en, direction='en-et')
52
+
53
+ # Create a recipe description
54
+ recipe_description = (
55
+ f"Retsept: {recipe_name_et}\n\n"
56
+ f"Link: {recipe_link}\n\n"
57
+ f"Koostisosad: {missed_ingredients_et}\n\n"
58
+ f"Valmistamise juhised: [Link to instructions]"
59
+ )
60
+
61
+ return recipe_description, recipe_link
62
+ else:
63
+ return "Viga retseptide toomisel.", ""
64
 
65
  # Create the Gradio interface
66
  iface = gr.Interface(
67
+ fn=find_recipes,
68
+ inputs=[
69
+ gr.Textbox(
70
+ lines=1,
71
+ placeholder="Sisestage koostisosa (nt lõhe, šokolaad)",
72
+ label="Koostisosa"
73
+ ),
74
+ gr.Radio(
75
+ ["Low Calories", "High Calories"],
76
+ label="Kalorsus"
77
+ ),
78
+ gr.Radio(
79
+ ["Contains Wheat", "Wheat Free"],
80
+ label="Nisusisaldus"
81
+ )
82
+ ],
83
+ outputs=[
84
+ gr.Textbox(lines=15, label="Genereeritud Retsept"),
85
+ gr.Textbox(lines=1, label="Retsepti Link")
86
+ ],
87
+ title="🍳 Nutikas Retsepti Generator",
88
+ description=(
89
+ "Sisestage koostisosa, valige kalorsus ja nisusisaldus. "
90
+ "Saate personaalse retsepti!"
91
+ ),
92
+ examples=[
93
+ ["lõhe", "Low Calories", "Wheat Free"],
94
+ ["šokolaadikook", "High Calories", "Contains Wheat"],
95
+ ["kartul", "Low Calories", "Wheat Free"]
96
+ ],
97
+ theme="default"
98
  )
99
 
100
  # Launch the interface
101
  if __name__ == "__main__":
102
  iface.launch()
103
+