adil9858's picture
Update app.py
325f2c0 verified
raw
history blame
2.26 kB
import streamlit as st
import requests
import base64
from PIL import Image
from io import BytesIO
# Function to encode an image into base64 format
def encode_image(img):
buffered = BytesIO()
img.save(buffered, format="PNG")
encoded_string = base64.b64encode(buffered.getvalue()).decode("utf-8")
return encoded_string
# Function to get explanation from VLM API
def explain_image_with_vlm(image):
api = "https://api.hyperbolic.xyz/v1/chat/completions"
api_key = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJhZGlsYXppejIwMTNAZ21haWwuY29tIiwiaWF0IjoxNzMyODU1NDI1fQ.lRjbz9LMW9jj7Lf7I8m_dTRh4KQ1wDCdWiTRGErMuEk"
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {api_key}",
}
base64_img = encode_image(image)
payload = {
"messages": [
{
"role": "user",
"content": [
{"type": "text", "text": "Explain the Image in 10 words only"},
{
"type": "image_url",
"image_url": {"url": f"data:image/jpeg;base64,{base64_img}"},
},
],
}
],
"model": "Qwen/Qwen2-VL-72B-Instruct",
"max_tokens": 2048,
"temperature": 0.7,
"top_p": 0.9,
}
response = requests.post(api, headers=headers, json=payload)
if response.status_code == 200:
return response.json().get("choices", [{}])[0].get("message", {}).get("content", "No explanation found.")
else:
return f"Error: {response.status_code} - {response.text}"
# Streamlit UI
st.title("πŸ“Έ AI-Powered Image Explainer")
st.subheader("Capture an image and let the AI explain it!")
# Camera input
img_file_buffer = st.camera_input("Take a picture")
if img_file_buffer:
# Display captured image
image = Image.open(img_file_buffer)
st.image(image, caption="Captured Image", use_column_width=True)
st.subheader("πŸ” Image Explanation")
with st.spinner("Analyzing image..."):
explanation = explain_image_with_vlm(image)
st.success("Analysis Complete!")
st.write(f"**Explanation:** {explanation}")
st.info(
"Developed by : DataScienceProF"
)