|
import base64 |
|
import requests |
|
import streamlit as st |
|
from io import BytesIO |
|
from PIL import Image |
|
|
|
|
|
def encode_image(img): |
|
buffered = BytesIO() |
|
img.save(buffered, format="PNG") |
|
encoded_string = base64.b64encode(buffered.getvalue()).decode("utf-8") |
|
return encoded_string |
|
|
|
|
|
def get_api_response(base64_img): |
|
api_url = "https://api.hyperbolic.xyz/v1/chat/completions" |
|
api_key = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJhZGlsYXppejIwMTNAZ21haWwuY29tIiwiaWF0IjoxNzMyODU1NDI1fQ.lRjbz9LMW9jj7Lf7I8m_dTRh4KQ1wDCdWiTRGErMuEk" |
|
headers = { |
|
"Content-Type": "application/json", |
|
"Authorization": f"Bearer {api_key}", |
|
} |
|
payload = { |
|
"messages": [ |
|
{ |
|
"role": "user", |
|
"content": [ |
|
{"type": "text", "text": "What is this image?"}, |
|
{ |
|
"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_url, headers=headers, json=payload) |
|
if response.status_code == 200: |
|
return response.json()['choices'][0]['message']['content'] |
|
else: |
|
return f"Error: Unable to get response from API {response.status_code}." |
|
|
|
|
|
def main(): |
|
st.title("Image Dex: AI Image Explainer") |
|
|
|
st.write("Upload an image and get a response based on the image content.") |
|
|
|
|
|
uploaded_file = st.file_uploader("Choose an image...", type=["png", "jpg", "jpeg"]) |
|
|
|
if uploaded_file is not None: |
|
|
|
img = Image.open(uploaded_file) |
|
|
|
|
|
st.image(img, caption="Uploaded Image",width=400) |
|
|
|
|
|
base64_img = encode_image(img) |
|
|
|
|
|
st.write("Processing the image...") |
|
response = get_api_response(base64_img) |
|
|
|
|
|
st.write("Result:-> ", response) |
|
|
|
|
|
if __name__ == "__main__": |
|
main() |
|
|