Kilos1 commited on
Commit
81ea024
·
verified ·
1 Parent(s): 88a82b5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -59
app.py CHANGED
@@ -3,33 +3,35 @@ import base64
3
  from transformers import AutoModelForCausalLM, AutoTokenizer
4
  import gradio as gr
5
  from PIL import Image
 
6
  from transformers import Owlv2Processor, Owlv2ForObjectDetection
7
 
8
  processor = Owlv2Processor.from_pretrained("google/owlv2-large-patch14-finetuned")
9
  model = Owlv2ForObjectDetection.from_pretrained("google/owlv2-large-patch14-finetuned")
10
 
11
- def input_image_setup(uploaded_file):
12
  """
13
  Encodes the uploaded image file into a base64 string.
14
 
15
  Parameters:
16
- - uploaded_file: File-like object uploaded via Gradio.
17
 
18
  Returns:
19
- - encoded_image (str): Base64 encoded string of the image data.
20
  """
21
- if uploaded_file is not None:
22
- # Convert the image to bytes and encode in Base64
23
- bytes_data = uploaded_file.tobytes()
24
- encoded_image = base64.b64encode(bytes_data).decode("utf-8")
 
 
25
  return encoded_image
26
  else:
27
  raise FileNotFoundError("No file uploaded")
28
 
29
  def format_response(response_text):
30
  """
31
- Formats the model response to display each item on a new line as a list.
32
- Converts numbered items into HTML `<ul>` and `<li>` format.
33
  """
34
  response_text = re.sub(r"\*\*(.*?)\*\*", r"<p><strong>\1</strong></p>", response_text)
35
  response_text = re.sub(r"(?m)^\s*\*\s(.*)", r"<li>\1</li>", response_text)
@@ -38,83 +40,56 @@ def format_response(response_text):
38
  response_text = re.sub(r"(\n|\\n)+", r"<br>", response_text)
39
  return response_text
40
 
41
- def generate_model_response(uploaded_file, user_query):
42
  """
43
  Processes the uploaded image and user query to generate a response from the model.
44
 
45
  Parameters:
46
- - uploaded_file: The uploaded image file.
47
  - user_query: The user's question about the image.
48
 
49
  Returns:
50
- - str: The generated response from the model.
51
  """
52
-
53
- # Encode the uploaded image into Base64 format
54
- encoded_image = input_image_setup(uploaded_file)
 
55
 
56
- # Define the assistant prompt
57
  assistant_prompt = """
58
- You are an expert nutritionist. Your task is to analyze the food items displayed in the image and provide a detailed nutritional assessment using the following format:
59
-
60
- 1. **Identification**: List each identified food item clearly, one per line.
61
- 2. **Portion Size & Calorie Estimation**: For each identified food item, specify the portion size and provide an estimated number of calories. Use bullet points with the following structure:
62
- - **[Food Item]**: [Portion Size], [Number of Calories] calories
63
-
64
- Example:
65
- * **Salmon**: 6 ounces, 210 calories
66
- * **Asparagus**: 3 spears, 25 calories
67
-
68
- 3. **Total Calories**: Provide the total number of calories for all food items.
69
-
70
- Example:
71
- Total Calories: [Number of Calories]
72
-
73
- 4. **Nutrient Breakdown**: Include a breakdown of key nutrients such as **Protein**, **Carbohydrates**, **Fats**, **Vitamins**, and **Minerals**. Use bullet points, and for each nutrient provide details about the contribution of each food item.
74
 
75
- Example:
76
- * **Protein**: Salmon (35g), Asparagus (3g), Tomatoes (1g) = [Total Protein]
 
 
 
 
77
 
78
- 5. **Health Evaluation**: Evaluate the healthiness of the meal in one paragraph.
79
-
80
- 6. **Disclaimer**: Include the following exact text as a disclaimer:
81
-
82
- The nutritional information and calorie estimates provided are approximate and are based on general food data.
83
- Actual values may vary depending on factors such as portion size, specific ingredients, preparation methods, and individual variations.
84
- For precise dietary advice or medical guidance, consult a qualified nutritionist or healthcare provider.
85
-
86
- Format your response exactly like the template above to ensure consistency.
87
  """
88
 
89
- # Prepare input for the model
90
  input_text = assistant_prompt + "\n\n" + user_query + "\n![Image](data:image/jpeg;base64," + encoded_image + ")"
91
-
92
- # Tokenize input text
93
  inputs = tokenizer(input_text, return_tensors="pt")
94
-
95
  try:
96
- # Generate response from the model
97
  outputs = model.generate(**inputs)
98
-
99
- # Decode and format the model's raw response
100
  raw_response = tokenizer.decode(outputs[0], skip_special_tokens=True)
101
  formatted_response = format_response(raw_response)
102
-
103
  return formatted_response
104
-
105
  except Exception as e:
106
  print(f"Error in generating response: {e}")
107
- return "An error occurred while generating the response."
108
 
109
- # Create Gradio interface
110
  iface = gr.Interface(
111
  fn=generate_model_response,
112
  inputs=[
113
- gr.Image(type="pil", label="Upload Image"), # Image upload component
114
- gr.Textbox(label="User Query", placeholder="Enter your question about the image...")
115
  ],
116
- outputs="html", # Display formatted HTML output
117
  )
118
 
119
- # Launch Gradio app
120
- iface.launch()
 
3
  from transformers import AutoModelForCausalLM, AutoTokenizer
4
  import gradio as gr
5
  from PIL import Image
6
+ import io
7
  from transformers import Owlv2Processor, Owlv2ForObjectDetection
8
 
9
  processor = Owlv2Processor.from_pretrained("google/owlv2-large-patch14-finetuned")
10
  model = Owlv2ForObjectDetection.from_pretrained("google/owlv2-large-patch14-finetuned")
11
 
12
+ def input_image_setup(image_file):
13
  """
14
  Encodes the uploaded image file into a base64 string.
15
 
16
  Parameters:
17
+ - image_file: Image file uploaded via Gradio
18
 
19
  Returns:
20
+ - encoded_image (str): Base64 encoded string of the image data
21
  """
22
+ if image_file is not None:
23
+ # Convert the PIL Image object to bytes and encode in Base64
24
+ buffered = io.BytesIO()
25
+ image_file.save(buffered, format="JPEG")
26
+ img_bytes = buffered.getvalue()
27
+ encoded_image = base64.b64encode(img_bytes).decode("utf-8")
28
  return encoded_image
29
  else:
30
  raise FileNotFoundError("No file uploaded")
31
 
32
  def format_response(response_text):
33
  """
34
+ Formats the model response to display each item as HTML elements.
 
35
  """
36
  response_text = re.sub(r"\*\*(.*?)\*\*", r"<p><strong>\1</strong></p>", response_text)
37
  response_text = re.sub(r"(?m)^\s*\*\s(.*)", r"<li>\1</li>", response_text)
 
40
  response_text = re.sub(r"(\n|\\n)+", r"<br>", response_text)
41
  return response_text
42
 
43
+ def generate_model_response(image_file, user_query):
44
  """
45
  Processes the uploaded image and user query to generate a response from the model.
46
 
47
  Parameters:
48
+ - image_file: The uploaded image file.
49
  - user_query: The user's question about the image.
50
 
51
  Returns:
52
+ - str: The generated response from the model, formatted as HTML.
53
  """
54
+ try:
55
+ encoded_image = input_image_setup(image_file)
56
+ except FileNotFoundError as e:
57
+ return f"<p>{str(e)}</p>"
58
 
 
59
  assistant_prompt = """
60
+ You are an expert nutritionist. Analyze the food items in the image and provide a detailed nutritional assessment:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
 
62
+ 1. **Identification**: List each food item.
63
+ 2. **Portion & Calories**: Specify portion size and calories for each item.
64
+ 3. **Total Calories**: Provide the total.
65
+ 4. **Nutrient Breakdown**: Detail key nutrients.
66
+ 5. **Health Evaluation**: Evaluate meal healthiness.
67
+ 6. **Disclaimer**: "Nutritional info is approximate. Consult a nutritionist for precise advice."
68
 
69
+ Format your response accordingly.
 
 
 
 
 
 
 
 
70
  """
71
 
 
72
  input_text = assistant_prompt + "\n\n" + user_query + "\n![Image](data:image/jpeg;base64," + encoded_image + ")"
73
+
 
74
  inputs = tokenizer(input_text, return_tensors="pt")
75
+
76
  try:
 
77
  outputs = model.generate(**inputs)
 
 
78
  raw_response = tokenizer.decode(outputs[0], skip_special_tokens=True)
79
  formatted_response = format_response(raw_response)
 
80
  return formatted_response
 
81
  except Exception as e:
82
  print(f"Error in generating response: {e}")
83
+ return f"<p>An error occurred: {str(e)}</p>"
84
 
85
+ # Gradio Interface
86
  iface = gr.Interface(
87
  fn=generate_model_response,
88
  inputs=[
89
+ gr.Image(type="pil", label="Upload Image"),
90
+ gr.Textbox(label="Enter your question", placeholder="How many calories are in this food?")
91
  ],
92
+ outputs=gr.HTML(label="Nutritional Assessment")
93
  )
94
 
95
+ iface.launch(true)