adil9858 commited on
Commit
325f2c0
Β·
verified Β·
1 Parent(s): e455b5a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -72
app.py CHANGED
@@ -1,72 +1,71 @@
1
- import streamlit as st
2
- import requests
3
- import base64
4
- from PIL import Image
5
- from io import BytesIO
6
-
7
- # Function to encode an image into base64 format
8
- def encode_image(img):
9
- buffered = BytesIO()
10
- img.save(buffered, format="PNG")
11
- encoded_string = base64.b64encode(buffered.getvalue()).decode("utf-8")
12
- return encoded_string
13
-
14
- # Function to get explanation from VLM API
15
- def explain_image_with_vlm(image):
16
- api = "https://api.hyperbolic.xyz/v1/chat/completions"
17
- api_key = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJhZGlsYXppejIwMTNAZ21haWwuY29tIiwiaWF0IjoxNzMyODU1NDI1fQ.lRjbz9LMW9jj7Lf7I8m_dTRh4KQ1wDCdWiTRGErMuEk"
18
-
19
- headers = {
20
- "Content-Type": "application/json",
21
- "Authorization": f"Bearer {api_key}",
22
- }
23
-
24
- base64_img = encode_image(image)
25
-
26
- payload = {
27
- "messages": [
28
- {
29
- "role": "user",
30
- "content": [
31
- {"type": "text", "text": "Explain the Image in 10 words only"},
32
- {
33
- "type": "image_url",
34
- "image_url": {"url": f"data:image/jpeg;base64,{base64_img}"},
35
- },
36
- ],
37
- }
38
- ],
39
- "model": "Qwen/Qwen2-VL-72B-Instruct",
40
- "max_tokens": 2048,
41
- "temperature": 0.7,
42
- "top_p": 0.9,
43
- }
44
-
45
- response = requests.post(api, headers=headers, json=payload)
46
- if response.status_code == 200:
47
- return response.json().get("choices", [{}])[0].get("message", {}).get("content", "No explanation found.")
48
- else:
49
- return f"Error: {response.status_code} - {response.text}"
50
-
51
- # Streamlit UI
52
- st.title("πŸ“Έ AI-Powered Image Explainer")
53
- st.subheader("Capture an image and let the AI explain it!")
54
-
55
- # Camera input
56
- img_file_buffer = st.camera_input("Take a picture")
57
-
58
- if img_file_buffer:
59
- # Display captured image
60
- image = Image.open(img_file_buffer)
61
- st.image(image, caption="Captured Image", use_column_width=True)
62
-
63
- st.subheader("πŸ” Image Explanation")
64
- with st.spinner("Analyzing image..."):
65
- explanation = explain_image_with_vlm(image)
66
- st.success("Analysis Complete!")
67
- st.write(f"**Explanation:** {explanation}")
68
-
69
- st.info(
70
- "This app captures an image using your device's camera and provides an AI-generated explanation "
71
- "using a cutting-edge Vision Language Model (VLM)."
72
- )
 
1
+ import streamlit as st
2
+ import requests
3
+ import base64
4
+ from PIL import Image
5
+ from io import BytesIO
6
+
7
+ # Function to encode an image into base64 format
8
+ def encode_image(img):
9
+ buffered = BytesIO()
10
+ img.save(buffered, format="PNG")
11
+ encoded_string = base64.b64encode(buffered.getvalue()).decode("utf-8")
12
+ return encoded_string
13
+
14
+ # Function to get explanation from VLM API
15
+ def explain_image_with_vlm(image):
16
+ api = "https://api.hyperbolic.xyz/v1/chat/completions"
17
+ api_key = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJhZGlsYXppejIwMTNAZ21haWwuY29tIiwiaWF0IjoxNzMyODU1NDI1fQ.lRjbz9LMW9jj7Lf7I8m_dTRh4KQ1wDCdWiTRGErMuEk"
18
+
19
+ headers = {
20
+ "Content-Type": "application/json",
21
+ "Authorization": f"Bearer {api_key}",
22
+ }
23
+
24
+ base64_img = encode_image(image)
25
+
26
+ payload = {
27
+ "messages": [
28
+ {
29
+ "role": "user",
30
+ "content": [
31
+ {"type": "text", "text": "Explain the Image in 10 words only"},
32
+ {
33
+ "type": "image_url",
34
+ "image_url": {"url": f"data:image/jpeg;base64,{base64_img}"},
35
+ },
36
+ ],
37
+ }
38
+ ],
39
+ "model": "Qwen/Qwen2-VL-72B-Instruct",
40
+ "max_tokens": 2048,
41
+ "temperature": 0.7,
42
+ "top_p": 0.9,
43
+ }
44
+
45
+ response = requests.post(api, headers=headers, json=payload)
46
+ if response.status_code == 200:
47
+ return response.json().get("choices", [{}])[0].get("message", {}).get("content", "No explanation found.")
48
+ else:
49
+ return f"Error: {response.status_code} - {response.text}"
50
+
51
+ # Streamlit UI
52
+ st.title("πŸ“Έ AI-Powered Image Explainer")
53
+ st.subheader("Capture an image and let the AI explain it!")
54
+
55
+ # Camera input
56
+ img_file_buffer = st.camera_input("Take a picture")
57
+
58
+ if img_file_buffer:
59
+ # Display captured image
60
+ image = Image.open(img_file_buffer)
61
+ st.image(image, caption="Captured Image", use_column_width=True)
62
+
63
+ st.subheader("πŸ” Image Explanation")
64
+ with st.spinner("Analyzing image..."):
65
+ explanation = explain_image_with_vlm(image)
66
+ st.success("Analysis Complete!")
67
+ st.write(f"**Explanation:** {explanation}")
68
+
69
+ st.info(
70
+ "Developed by : DataScienceProF"
71
+ )