shukdevdatta123 commited on
Commit
5016e38
·
verified ·
1 Parent(s): 5b332f1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -29
app.py CHANGED
@@ -1,32 +1,35 @@
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
- max_completion_tokens=300, # Use max_completion_tokens instead of max_tokens
28
- reasoning_effort=reasoning_effort # Include reasoning_effort in the request
29
  )
 
30
  return response["choices"][0]["message"]["content"]
31
  except Exception as e:
32
  return f"Error calling OpenAI API: {str(e)}"
@@ -42,19 +45,7 @@ def get_base64_string_from_image(pil_image):
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 (this does NOT analyze the image directly)
48
- if image:
49
- try:
50
- # Ensure the image is a PIL object (Gradio returns a PIL Image object)
51
- image = Image.open(image)
52
- image_info = get_base64_string_from_image(image) # Store base64 string for the image
53
- except Exception as e:
54
- image_info = f"Error reading image: {e}"
55
-
56
- # Combine user input with image info (if any)
57
- response = generate_response(input_text, openai_api_key, image_info, reasoning_effort)
58
 
59
  # Append the response to the history
60
  history.append((f"User: {input_text}", f"Assistant: {response}"))
@@ -97,4 +88,4 @@ def create_interface():
97
  # Run the interface
98
  if __name__ == "__main__":
99
  demo = create_interface()
100
- demo.launch()
 
1
  import gradio as gr
2
  import openai
3
+ import base64
4
  from PIL import Image
5
  import io
 
6
 
7
+ # Function to send the request to OpenAI API with an image or text input
8
+ def generate_response(input_text, image, openai_api_key, 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
+ # Process the input depending on whether it's text or an image
15
+ if image:
16
+ # Convert the image to base64 string
17
+ image_info = get_base64_string_from_image(image)
18
+ input_text = f"data:image/png;base64,{image_info}"
19
+
20
+ # Prepare the messages for OpenAI API
21
+ messages = [
22
+ {"role": "user", "content": [{"type": "image_url", "image_url": {"url": input_text}}]}
23
+ ]
24
 
25
  try:
26
+ # Call OpenAI API with the "o1" model
27
  response = openai.ChatCompletion.create(
28
+ model="o1", # Using model "o1"
29
+ messages=messages,
30
+ reasoning_effort=reasoning_effort # Set reasoning_effort for the response
 
 
 
 
31
  )
32
+
33
  return response["choices"][0]["message"]["content"]
34
  except Exception as e:
35
  return f"Error calling OpenAI API: {str(e)}"
 
45
 
46
  # The function that will be used by Gradio interface
47
  def chatbot(input_text, image, openai_api_key, reasoning_effort, history=[]):
48
+ response = generate_response(input_text, image, openai_api_key, reasoning_effort)
 
 
 
 
 
 
 
 
 
 
 
 
49
 
50
  # Append the response to the history
51
  history.append((f"User: {input_text}", f"Assistant: {response}"))
 
88
  # Run the interface
89
  if __name__ == "__main__":
90
  demo = create_interface()
91
+ demo.launch()