Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import base64
|
2 |
+
import requests
|
3 |
+
import streamlit as st
|
4 |
+
from io import BytesIO
|
5 |
+
from PIL import Image
|
6 |
+
|
7 |
+
# Function to encode image into base64
|
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 interact with the Hyperbolic API
|
15 |
+
def get_api_response(base64_img):
|
16 |
+
api_url = "https://api.hyperbolic.xyz/v1/chat/completions"
|
17 |
+
api_key = "YOUR_API_KEY" # Replace with your actual API key
|
18 |
+
headers = {
|
19 |
+
"Content-Type": "application/json",
|
20 |
+
"Authorization": f"Bearer {api_key}",
|
21 |
+
}
|
22 |
+
payload = {
|
23 |
+
"messages": [
|
24 |
+
{
|
25 |
+
"role": "user",
|
26 |
+
"content": [
|
27 |
+
{"type": "text", "text": "What is this image?"},
|
28 |
+
{
|
29 |
+
"type": "image_url",
|
30 |
+
"image_url": {"url": f"data:image/jpeg;base64,{base64_img}"},
|
31 |
+
},
|
32 |
+
],
|
33 |
+
}
|
34 |
+
],
|
35 |
+
"model": "Qwen/Qwen2-VL-72B-Instruct",
|
36 |
+
"max_tokens": 2048,
|
37 |
+
"temperature": 0.7,
|
38 |
+
"top_p": 0.9,
|
39 |
+
}
|
40 |
+
|
41 |
+
response = requests.post(api_url, headers=headers, json=payload)
|
42 |
+
if response.status_code == 200:
|
43 |
+
return response.json()['choices'][0]['message']['content']
|
44 |
+
else:
|
45 |
+
return "Error: Unable to get response from API."
|
46 |
+
|
47 |
+
# Streamlit interface
|
48 |
+
def main():
|
49 |
+
st.title("Image to Text AI Application")
|
50 |
+
st.write("Upload an image and get a response based on the image content.")
|
51 |
+
|
52 |
+
# File uploader for image
|
53 |
+
uploaded_file = st.file_uploader("Choose an image...", type=["png", "jpg", "jpeg"])
|
54 |
+
|
55 |
+
if uploaded_file is not None:
|
56 |
+
# Open the uploaded image
|
57 |
+
img = Image.open(uploaded_file)
|
58 |
+
|
59 |
+
# Display the uploaded image
|
60 |
+
st.image(img, caption="Uploaded Image", use_column_width=True)
|
61 |
+
|
62 |
+
# Encode the image to base64
|
63 |
+
base64_img = encode_image(img)
|
64 |
+
|
65 |
+
# Get response from the API
|
66 |
+
st.write("Processing the image...")
|
67 |
+
response = get_api_response(base64_img)
|
68 |
+
|
69 |
+
# Display the result
|
70 |
+
st.write("API Response: ", response)
|
71 |
+
|
72 |
+
# Run the Streamlit app
|
73 |
+
if __name__ == "__main__":
|
74 |
+
main()
|