shukdevdatta123 commited on
Commit
eaa4360
·
verified ·
1 Parent(s): 30fb1f4

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +100 -0
app.py ADDED
@@ -0,0 +1,100 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import openai
3
+ from PIL import Image
4
+ import io
5
+ import base64
6
+
7
+ # Function to send the request to OpenAI API
8
+ def generate_response(prompt, openai_api_key, image_info="", reasoning_effort="medium"):
9
+ if not openai_api_key:
10
+ return "Error: No API key provided."
11
+
12
+ openai.api_key = openai_api_key
13
+
14
+ # Combine text prompt with optional image info
15
+ full_prompt = prompt
16
+ if image_info:
17
+ full_prompt += f"\n\nAdditional context about the image: {image_info}"
18
+
19
+ try:
20
+ # Call OpenAI API with the specified model ("o1")
21
+ response = openai.ChatCompletion.create(
22
+ model="o1", # use model "o1"
23
+ messages=[
24
+ {"role": "system", "content": "You are a helpful assistant."},
25
+ {"role": "user", "content": full_prompt},
26
+ ],
27
+ temperature=0.7,
28
+ max_tokens=300,
29
+ reasoning_effort=reasoning_effort # Include reasoning_effort in the request
30
+ )
31
+ return response["choices"][0]["message"]["content"]
32
+ except Exception as e:
33
+ return f"Error calling OpenAI API: {str(e)}"
34
+
35
+ # Function to convert an uploaded image to a base64 string
36
+ def get_base64_string_from_image(pil_image):
37
+ buffered = io.BytesIO()
38
+ pil_image.save(buffered, format="PNG")
39
+ img_bytes = buffered.getvalue()
40
+ base64_str = base64.b64encode(img_bytes).decode("utf-8")
41
+ return base64_str
42
+
43
+ # The function that will be used by Gradio interface
44
+ def chatbot(input_text, image, openai_api_key, reasoning_effort, history=[]):
45
+ image_info = ""
46
+
47
+ # If an image is uploaded, convert it to base64 for reference
48
+ if image:
49
+ try:
50
+ image = Image.open(image)
51
+ image_info = get_base64_string_from_image(image)
52
+ except Exception as e:
53
+ image_info = f"Error reading image: {e}"
54
+
55
+ # Combine user input with image info (if any)
56
+ response = generate_response(input_text, openai_api_key, image_info, reasoning_effort)
57
+
58
+ # Append the response to the history
59
+ history.append((f"User: {input_text}", f"Assistant: {response}"))
60
+
61
+ return "", history
62
+
63
+ # Function to clear the chat history
64
+ def clear_history():
65
+ return "", []
66
+
67
+ # Gradio interface setup
68
+ def create_interface():
69
+ with gr.Blocks() as demo:
70
+ gr.Markdown("# Multimodal Chatbot (Text + Image)")
71
+
72
+ with gr.Row():
73
+ openai_api_key = gr.Textbox(label="Enter OpenAI API Key", type="password", placeholder="sk-...", interactive=True)
74
+
75
+ with gr.Row():
76
+ image_input = gr.Image(label="Upload an Image", type="pil")
77
+ input_text = gr.Textbox(label="Enter Text Question", placeholder="Ask a question or provide text", lines=2)
78
+
79
+ with gr.Row():
80
+ reasoning_effort = gr.Dropdown(
81
+ label="Reasoning Effort",
82
+ choices=["low", "medium", "high"],
83
+ value="medium",
84
+ description="Select the reasoning effort for generating the response."
85
+ )
86
+ submit_btn = gr.Button("Send")
87
+ clear_btn = gr.Button("Clear History")
88
+
89
+ chat_history = gr.Chatbot()
90
+
91
+ # Button interactions
92
+ submit_btn.click(fn=chatbot, inputs=[input_text, image_input, openai_api_key, reasoning_effort, chat_history], outputs=[input_text, chat_history])
93
+ clear_btn.click(fn=clear_history, inputs=[], outputs=[chat_history, chat_history])
94
+
95
+ return demo
96
+
97
+ # Run the interface
98
+ if __name__ == "__main__":
99
+ demo = create_interface()
100
+ demo.launch()