Spaces:
Runtime error
Runtime error
| 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() |