Spaces:
Runtime error
Runtime error
Create app.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081import gradio as grimport base64import requestsimport tempfileimport osimport 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." prompt = "I want you to act as a friendly tile setters who has 10 years of experience with floor tiling. I will share an image of floor with you and you will reply to me with the number of tiles that need cleaning. Reply with accuracy and compassion. I want you to keep the reply neat, limiting the reply to 50 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()app.pyAdded+81