Xhaheen commited on
Commit
6fff1ff
ยท
verified ยท
1 Parent(s): d17b1dd

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -0
app.py ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ from PIL import Image
4
+ from openai import OpenAI
5
+ import os
6
+ import base64
7
+ import io
8
+ import glob
9
+
10
+ client = OpenAI(
11
+ api_key=os.getenv('API_KEY'),
12
+ base_url=os.getenv('API_BASE'))
13
+
14
+ MODELS = [
15
+ "google/gemini-flash-1.5",
16
+ "qwen/qwen-2.5-72b-instruct",
17
+ "mistralai/pixtral-12b:free"
18
+ ]
19
+
20
+ def encode_image(image):
21
+ buffered = io.BytesIO()
22
+ image.save(buffered, format="PNG")
23
+ return base64.b64encode(buffered.getvalue()).decode('utf-8')
24
+
25
+ def analyze_label(image, model):
26
+ if image is None:
27
+ return "Please upload an image of a product label, food item, or menu."
28
+
29
+ image.thumbnail((1024, 1024), Image.LANCZOS)
30
+ base64_image = encode_image(image)
31
+
32
+ try:
33
+ response = client.chat.completions.create(
34
+ model=model,
35
+ messages=[{
36
+ "role": "user",
37
+ "content": [
38
+ {"type": "text", "text": "Analyze this image of a product label, food item, or menu. Provide a simple description of the product and its nutritional information, (for images of food / fruits / dishes / always give approximate calorie values in number with disclaimer that it's generic and it may vary), consider user as if talking to someone with no nutritional background."},
39
+ {"type": "image_url", "image_url": {"url": f"data:image/png;base64,{base64_image}"}}
40
+ ]
41
+ }],
42
+ max_tokens=1000,
43
+ temperature=0.01
44
+ )
45
+ return response.choices[0].message.content
46
+ except Exception as e:
47
+ return f"Error analyzing image: {str(e)}"
48
+
49
+ # Get example images
50
+ example_images = glob.glob("*.png")
51
+
52
+ iface = gr.Interface(
53
+ fn=analyze_label,
54
+ inputs=[
55
+ gr.Image(type="pil", label="Upload a picture of a product label, food item, or menu"),
56
+ gr.Dropdown(choices=MODELS, label="Select Model")
57
+ ],
58
+ outputs=gr.Textbox(label="Analysis"),
59
+ title="๐Ÿ Sehat GPT ๐Ÿฅฆ",
60
+ description="Sehat GPT: Your AI nutritionist, inspired by the Label Padhega India campaign. ๐Ÿค–๐Ÿฅ— Upload food images for instant nutrition insights and make informed dietary choices. Empowering you to read labels, understand nutrition, and lead a healthier life! ๐Ÿ’ช๐ŸŽ Join the movement to promote food literacy and consumer awareness across India. ๐Ÿ‡ฎ๐Ÿ‡ณ",
61
+ examples=[[img, MODELS[0]] for img in example_images]
62
+ )
63
+
64
+ # Remove the launch command for Gradio Spaces
65
+ # iface.launch(debug=True)