forked from Nedzhin/hope_base
-
Notifications
You must be signed in to change notification settings - Fork 0
/
about.py
70 lines (61 loc) · 2.39 KB
/
about.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import base64
import streamlit as st
import openai
import requests
from PIL import Image
import os
# Set your OpenAI API key here
api_key = os.getenv('OPENAI_API_KEY')
def encode_image(image_path):
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode('utf-8')
def save_uploaded_file(uploaded_file, target_path):
with open(target_path, "wb") as f:
f.write(uploaded_file.getbuffer())
def app():
st.title("Image Explanation Chatbot")
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
if uploaded_file is not None:
image = Image.open(uploaded_file)
st.image(image, caption="Uploaded Image", use_column_width=True)
save_uploaded_file(uploaded_file, 'temp.jpg')
base64_image = encode_image('temp.jpg')
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {api_key}"
}
payload = {
"model": "gpt-4-vision-preview",
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": "What’s in this image? Explain the image content"
},
{
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{base64_image}"
}
}
]
}
],
"max_tokens": 300
}
if st.button("Get Explanation"):
try:
# Call OpenAI API for image explanation
# response = openai.Image.create(
# file=uploaded_file,
# model="text-davinci-003"
# )
# # Extract explanation from OpenAI API response
# explanation = response['data'][0]['text']
response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload)
print(response.json())
st.success("Explanation: {}".format(response.json()['choices'][0]['message']['content']))
except Exception as e:
st.error(f"Error: {e}")