Spaces:
Runtime error
Runtime error
Update app.py
Browse files2 unmodified lines34567891011121314151617181920213 unmodified lines252627282930314 unmodified lines363738394041424344454647484950515253545556575859606162636465662 unmodified linesimport requestsimport tempfileimport osdef encode_image(image_path):with open(image_path, "rb") as image_file:return base64.b64encode(image_file.read()).decode('utf-8')def upload_img(image,api_key,img_ques):base64_image = encode_image(image)headers = {"Content-Type": "application/json","Authorization": f"Bearer {api_key}"}payload = {"model": "gpt-4-vision-preview",3 unmodified lines"content": [{"type": "text","text": img_ques},{"type": "image_url",4 unmodified lines]}],"max_tokens": 300}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"]title = """<h1 align="center">GPT-4 Image Preview</h1>"""with gr.Blocks() as demo:gr.Markdown(title)with gr.Row():with gr.Column(scale=0.5):image_input = gr.Image(type="filepath", label="Upload Image")key_input = gr.Textbox(label="Enter API-key")upload_button = gr.Button(value="Upload & Start Chat", interactive=True, variant="primary")btn_clear = gr.ClearButton([image_input,key_input])with gr.Column():text = gr.Textbox(label="Output")ques_input = gr.Textbox(label="Enter the question")ques_clear = gr.ClearButton([ques_input])upload_button.click(upload_img, inputs=[image_input,key_input,ques_input], outputs=text)btn_clear.add(text)ques_clear.add(text)demo.launch(share=True)2 unmodified lines3456789101112131415161718192021222324253 unmodified lines293031323334354 unmodified lines404142434445464748495051525354555657585960616263646566676869707172737475767778792 unmodified linesimport requestsimport tempfileimport osimport openaiprompt = "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 = "Provide a brief description of the image."headers = {"Content-Type": "application/json","Authorization": f"Bearer {api_key}"}payload = {"model": "gpt-4-vision-preview",3 unmodified lines"content": [{"type": "text","text": prompt},{"type": "image_url",4 unmodified lines]}],"max_tokens": 500}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, historyblock = gr.Blocks(theme=gr.themes.Monochrome())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)btn_clear = gr.ClearButton([message])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])btn_clear.add(message)block.launch(share = True)
app.py+35-22