Arfa-ilyas commited on
Commit
fdce92b
·
verified ·
1 Parent(s): 73d4557

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +108 -0
app.py ADDED
@@ -0,0 +1,108 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import streamlit as st
3
+ from transformers import VisionEncoderDecoderModel, ViTImageProcessor, AutoTokenizer
4
+ import torch
5
+ from PIL import Image
6
+ import requests
7
+ import os
8
+
9
+
10
+ # Load the model and tokenizer
11
+ model = VisionEncoderDecoderModel.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
12
+ feature_extractor = ViTImageProcessor.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
13
+ tokenizer = AutoTokenizer.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
14
+
15
+
16
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
17
+ model.to(device)
18
+
19
+
20
+ # Define the image captioning function
21
+ def generate_caption(image):
22
+ pixel_values = feature_extractor(images=image, return_tensors="pt").pixel_values
23
+ pixel_values = pixel_values.to(device)
24
+
25
+
26
+ output_ids = model.generate(pixel_values, max_length=16, num_beams=4)
27
+ caption = tokenizer.batch_decode(output_ids, skip_special_tokens=True)[0]
28
+ return caption.strip()
29
+
30
+
31
+ # Function to fetch product information from GROQ API
32
+ def fetch_product_info(caption):
33
+ # Define your GROQ API endpoint
34
+ api_url = "GroqCloud" # Replace with your actual GROQ API URL
35
+
36
+
37
+
38
+ # Retrieve the API key from environment variables
39
+ api_key = os.getenv('groq_api') # Use environment variable for API key
40
+
41
+
42
+ if not api_key:
43
+ st.error("API key not found. Set the 'GROQ_API_KEY' environment variable.")
44
+ return None
45
+
46
+
47
+ # Set up the headers with the Bearer token
48
+ headers = {"Authorization": f"Bearer {groq_api}"}
49
+
50
+
51
+ # Query the API
52
+ params = {"query": caption}
53
+ try:
54
+ response = requests.get(api_url, headers=headers, params=params)
55
+ response.raise_for_status() # Raise an exception for HTTP errors
56
+
57
+
58
+ data = response.json()
59
+ if data:
60
+ product = data.get('products', [])[0] # Assuming the first product is relevant
61
+ ingredients = product.get('ingredients', 'N/A')
62
+ usage = product.get('usage', 'N/A')
63
+ barcode = product.get('barcode', 'N/A')
64
+ return ingredients, usage, barcode
65
+ else:
66
+ st.write("No products found in the response.")
67
+ return None
68
+ except requests.RequestException as e:
69
+ st.error(f"Error fetching data from GROQ API: {e}")
70
+ return None
71
+
72
+
73
+ # Streamlit UI
74
+ st.title("Image Captioning and Product Information")
75
+ st.write("Upload an image to get a caption and related product information.")
76
+
77
+
78
+ # Upload image
79
+ uploaded_image = st.file_uploader("Choose an image...", type=["jpg", "png", "jpeg"])
80
+
81
+
82
+ if uploaded_image:
83
+ image = Image.open(uploaded_image).convert("RGB")
84
+ st.image(image, caption="Uploaded Image", use_column_width=True)
85
+
86
+ # Generate and display caption
87
+ with st.spinner("Generating caption..."):
88
+ caption = generate_caption(image)
89
+ st.write(f"**Caption:** {caption}")
90
+
91
+
92
+ # Fetch and display additional information from GROQ API
93
+ info = fetch_product_info(caption)
94
+
95
+ if info:
96
+ ingredients, usage, barcode = info
97
+ st.write("### Additional Information:")
98
+ st.write(f"**Ingredients:** {ingredients}")
99
+ st.write(f"**Usage:** {usage}")
100
+ st.write(f"**Barcoding:** {barcode}")
101
+ else:
102
+ st.write("No additional information found for this product.")
103
+
104
+
105
+
106
+
107
+
108
+