Spaces:
Runtime error
Runtime error
File size: 3,035 Bytes
a98b175 1f99ed0 a98b175 |
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 70 71 72 73 74 75 76 77 78 79 80 |
import gradio as gr
import base64
import requests
import tempfile
import os
import openai
prompt = "Type and press Enter"
def encode_image(image_path):
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode('utf-8')
def api_calling(image, prompt, api_key):
base64_image = encode_image(image)
if len(prompt) == 0:
prompt = "I want you to act as my friend. I will share images with you and ask about my queries related to the images and you will reply to me with the explanation of the image with accuracy, compassion and in conversational manor. I want you to keep the reply neat, limiting the reply to 200 words in English."
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {api_key}"
}
payload = {
"model": "gpt-4-vision-preview",
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": prompt
},
{
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{base64_image}",
"detail": "high"
}
}
]
}
],
"max_tokens": 500,
"temperature": 0.5,
"n": 1
}
response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload)
img_preview = response.json()
return img_preview["choices"][0]["message"]["content"]
def message_and_history(img, input, history, api_key):
history = history or []
s = list(sum(history, ()))
s.append(input)
inp = ' '.join(s)
output = api_calling(img,inp, api_key)
if len(input) == 0:
input = "Brief description of the image."
history.append((input, output))
else:
history.append((input, output))
return history, history
block = gr.Blocks(theme=gr.themes.Glass(primary_hue="slate"))
with block:
gr.Markdown("""<h1><center>Visual Explorer - Ask and Learn about Images</center></h1> """)
with gr.Row():
with gr.Column(scale=0.5):
img_input = gr.Image(type="filepath", label="Upload Image")
api_input = gr.Textbox(label= "Enter Api-key")
upload_button = gr.Button(value="Upload & Start Chat", interactive=True, variant="primary")
with gr.Column():
chatbot = gr.Chatbot(label="Chat with Image")
message = gr.Textbox(label="User", placeholder=prompt)
state = gr.State()
upload_button.click(message_and_history, inputs=[img_input, message, state, api_input], outputs=[chatbot, state])
message.submit(message_and_history, inputs=[img_input, message, state, api_input], outputs=[chatbot, state])
message.submit(lambda: None, None, message, queue=False)
block.launch() |