irins commited on
Commit
9a2d557
·
verified ·
1 Parent(s): ab45f73

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -4
app.py CHANGED
@@ -1,7 +1,65 @@
 
1
  import gradio as gr
 
2
 
3
- def greet(name):
4
- return "Hello " + name + "!"
 
5
 
6
- demo = gr.Interface(fn=greet, inputs="text", outputs="text")
7
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
  import gradio as gr
3
+ import requests
4
 
5
+ # Get API keys from Secrets
6
+ spoonacular_api_key = os.getenv('SPOONACULAR_API_KEY') # Spoonacular API key
7
+ hf_api_key = os.getenv('HF_API_KEY') # Hugging Face API key
8
 
9
+ # Translation function using Hugging Face
10
+ def translate_text_hf(text):
11
+ url = "https://api-inference.huggingface.co/models/Helsinki-NLP/opus-mt-et-en"
12
+ headers = {"Authorization": f"Bearer {hf_api_key}"}
13
+ payload = {"inputs": text}
14
+ response = requests.post(url, headers=headers, json=payload)
15
+ if response.status_code == 200:
16
+ return response.json()[0]['translation_text']
17
+ else:
18
+ return "Translation failed!"
19
+
20
+ # Recipe search function using Spoonacular API
21
+ def get_recipes(ingredients, max_calories=None, min_protein=None, max_fat=None, max_carbs=None):
22
+ translated_ingredients = translate_text_hf(ingredients)
23
+ url = "https://api.spoonacular.com/recipes/complexSearch"
24
+ params = {
25
+ "query": translated_ingredients,
26
+ "apiKey": spoonacular_api_key,
27
+ "maxCalories": max_calories,
28
+ "minProtein": min_protein,
29
+ "maxFat": max_fat,
30
+ "maxCarbs": max_carbs,
31
+ "number": 5
32
+ }
33
+ response = requests.get(url, params=params)
34
+ if response.status_code == 200:
35
+ recipes = response.json().get('results', [])
36
+ if recipes:
37
+ return "\n".join(
38
+ [f"Recipe: {recipe['title']} (Link: https://spoonacular.com/recipes/{recipe['id']})"
39
+ for recipe in recipes]
40
+ )
41
+ else:
42
+ return "No suitable recipes found."
43
+ else:
44
+ return f"Error fetching recipes: {response.status_code}"
45
+
46
+ # Gradio Interface
47
+ def recipe_app(ingredients, max_calories, min_protein, max_fat, max_carbs):
48
+ return get_recipes(ingredients, max_calories, min_protein, max_fat, max_carbs)
49
+
50
+ # Create Gradio interface
51
+ interface = gr.Interface(
52
+ fn=recipe_app,
53
+ inputs=[
54
+ gr.Textbox(label="Enter ingredients in Estonian :)/Sisestage koostisosad eesti keeles"),
55
+ gr.Number(label="Maximum calories (optional)", placeholder="e.g., 500"),
56
+ gr.Number(label="Minimum protein (g, optional)", placeholder="e.g., 10"),
57
+ gr.Number(label="Maximum fat (g, optional)", placeholder="e.g., 20"),
58
+ gr.Number(label="Maximum carbs (g, optional)", placeholder="e.g., 50"),
59
+ ],
60
+ outputs="text",
61
+ title="Estonian Recipe Search",
62
+ description="Enter ingredients in Estonian and set optional limits for calories, protein, fat, and carbs to find suitable recipes."
63
+ )
64
+
65
+ interface.launch()