Anupam251272 commited on
Commit
915e496
·
verified ·
1 Parent(s): 78ae90c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -10
app.py CHANGED
@@ -8,6 +8,10 @@ from datetime import datetime
8
  load_dotenv()
9
  HF_TOKEN = os.getenv("HF_TOKEN")
10
 
 
 
 
 
11
  # Initialize the HuggingFace inference endpoint
12
  llm = HuggingFaceEndpoint(
13
  repo_id="mistralai/Mistral-7B-Instruct-v0.3",
@@ -17,6 +21,9 @@ llm = HuggingFaceEndpoint(
17
 
18
  # Input validation function
19
  def validate_ingredients(ingredients):
 
 
 
20
  prompt = (
21
  f"Review the provided list of items: {ingredients}. "
22
  f"Determine if all items are valid food ingredients. "
@@ -38,18 +45,24 @@ def generate_recipe(ingredients):
38
 
39
  # Combined function for Gradio
40
  def suggest_recipes(ingredients):
 
 
 
41
  validation_result = validate_ingredients(ingredients)
42
- if validation_result == "Valid":
43
  return generate_recipe(ingredients)
44
  else:
45
- return "I'm sorry, but I can't process this request due to invalid ingredients. Please provide valid ingredients for cooking!"
46
 
47
  # Feedback function
48
  def save_feedback(feedback):
49
  if feedback.strip():
50
- with open("feedback.txt", "a") as file:
51
- file.write(f"{datetime.now()}: {feedback}\n")
52
- return "Thank you for your feedback!"
 
 
 
53
  return "Please enter feedback before submitting."
54
 
55
  # Gradio interface with professional color theme
@@ -59,7 +72,7 @@ with gr.Blocks(theme=gr.themes.Monochrome(primary_hue="blue")) as app:
59
  gr.HTML(
60
  """
61
  <div style="text-align: center; margin: auto;">
62
- <h1 style="color: #1155ff; font-size: 3.5em; font-weight: bold; font-family: 'Arial', sans-serif; margin-bottom: 0.5em;">🍳 Recipe Generator !</h1>
63
  <p style="font-size: 1.5em; color: #555555; font-weight: lighter; font-family: 'Verdana', sans-serif;">
64
  Enter the ingredients you have, and we'll validate them and suggest delightful recipes!
65
  </p>
@@ -67,9 +80,13 @@ with gr.Blocks(theme=gr.themes.Monochrome(primary_hue="blue")) as app:
67
  """
68
  )
69
 
70
- # Banner Image
71
- with gr.Row():
72
- gr.Image("./recipe-generator-banner.png", show_label=False, elem_id="banner-image", width="100%")
 
 
 
 
73
 
74
  # Ingredient input and output with borders and padding for more contrast
75
  with gr.Row():
@@ -182,4 +199,5 @@ with gr.Blocks(theme=gr.themes.Monochrome(primary_hue="blue")) as app:
182
  """
183
 
184
  # Launch the app
185
- app.launch()
 
 
8
  load_dotenv()
9
  HF_TOKEN = os.getenv("HF_TOKEN")
10
 
11
+ # Check if token exists and handle error gracefully
12
+ if not HF_TOKEN:
13
+ raise ValueError("HF_TOKEN environment variable not found. Please set it in your .env file.")
14
+
15
  # Initialize the HuggingFace inference endpoint
16
  llm = HuggingFaceEndpoint(
17
  repo_id="mistralai/Mistral-7B-Instruct-v0.3",
 
21
 
22
  # Input validation function
23
  def validate_ingredients(ingredients):
24
+ if not ingredients or ingredients.strip() == "":
25
+ return "Invalid"
26
+
27
  prompt = (
28
  f"Review the provided list of items: {ingredients}. "
29
  f"Determine if all items are valid food ingredients. "
 
45
 
46
  # Combined function for Gradio
47
  def suggest_recipes(ingredients):
48
+ if not ingredients or ingredients.strip() == "":
49
+ return "Please enter ingredients before submitting."
50
+
51
  validation_result = validate_ingredients(ingredients)
52
+ if "Valid" in validation_result: # More flexible check for "Valid" in response
53
  return generate_recipe(ingredients)
54
  else:
55
+ return "I'm sorry, but I can't process this request due to invalid ingredients. Please provide valid food ingredients for cooking!"
56
 
57
  # Feedback function
58
  def save_feedback(feedback):
59
  if feedback.strip():
60
+ try:
61
+ with open("feedback.txt", "a") as file:
62
+ file.write(f"{datetime.now()}: {feedback}\n")
63
+ return "Thank you for your feedback!"
64
+ except Exception as e:
65
+ return f"Error saving feedback: {str(e)}"
66
  return "Please enter feedback before submitting."
67
 
68
  # Gradio interface with professional color theme
 
72
  gr.HTML(
73
  """
74
  <div style="text-align: center; margin: auto;">
75
+ <h1 style="color: #1155ff; font-size: 3.5em; font-weight: bold; font-family: 'Arial', sans-serif; margin-bottom: 0.5em;">🍳 Recipe Generator</h1>
76
  <p style="font-size: 1.5em; color: #555555; font-weight: lighter; font-family: 'Verdana', sans-serif;">
77
  Enter the ingredients you have, and we'll validate them and suggest delightful recipes!
78
  </p>
 
80
  """
81
  )
82
 
83
+ # Banner Image - Check if file exists first
84
+ try:
85
+ with gr.Row():
86
+ gr.Image("./recipe-generator-banner.png", show_label=False, elem_id="banner-image", width="100%")
87
+ except Exception:
88
+ # Skip banner if image doesn't exist
89
+ pass
90
 
91
  # Ingredient input and output with borders and padding for more contrast
92
  with gr.Row():
 
199
  """
200
 
201
  # Launch the app
202
+ if __name__ == "__main__":
203
+ app.launch()