cyberandy commited on
Commit
397f6ad
·
verified ·
1 Parent(s): 65d6d47

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -33
app.py CHANGED
@@ -1,56 +1,63 @@
 
 
1
  import gradio as gr
2
- from openai import OpenAI
3
- import os
4
  from PIL import Image
5
  import numpy as np
6
  from datetime import datetime
 
7
 
8
- # Initialize OpenAI client
9
- client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
10
-
11
-
12
- def array_to_image_path(image_array):
13
- if image_array is None:
14
- raise ValueError("No image provided. Please upload an image before submitting.")
15
 
 
 
 
16
  img = Image.fromarray(np.uint8(image_array))
17
- timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
18
- filename = f"image_{timestamp}.png"
19
- img.save(filename)
20
- full_path = os.path.abspath(filename)
21
- return full_path
22
-
23
 
24
  # Function to generate product description using OpenAI API
25
  def generate_product_description(image, text_input=None):
26
- # Convert the image to a path (simulated handling)
27
- image_path = array_to_image_path(image)
28
 
29
- # Use a generated link from the Gradio app
30
- # If share=True is set, the image will be available via a public URL
31
- # Make sure the provided URL is accessible and correct in context
32
- image_url = image_path # Replace this with actual URL when using Gradio's share functionality
33
 
34
- # API request
35
- completion = client.chat.completions.create(
36
- model="gpt-4o",
37
- messages=[
38
  {
39
  "role": "user",
40
  "content": [
41
  {"type": "text", "text": text_input or "What's in this image?"},
42
  {
43
  "type": "image_url",
44
- "image_url": {"url": image_url},
45
- },
46
- ],
 
 
47
  }
48
  ],
49
- )
 
50
 
51
- # Extract and return the generated message
52
- return completion.choices[0].message
53
 
 
 
 
 
 
 
54
 
55
  css = """
56
  #output {
@@ -75,6 +82,6 @@ with gr.Blocks(css=css) as demo:
75
  generate_product_description, [input_img, text_input], [output_text]
76
  )
77
 
78
- # Set share=True to create a public URL
79
  demo.queue(api_open=False)
80
- demo.launch(debug=True, share=True)
 
1
+ import base64
2
+ import requests
3
  import gradio as gr
 
 
4
  from PIL import Image
5
  import numpy as np
6
  from datetime import datetime
7
+ import os
8
 
9
+ # OpenAI API Key
10
+ api_key = os.getenv("OPENAI_API_KEY")
 
 
 
 
 
11
 
12
+ # Function to encode the image
13
+ def encode_image(image_array):
14
+ # Convert numpy array to an image file and encode it in base64
15
  img = Image.fromarray(np.uint8(image_array))
16
+ img_buffer = os.path.join("/tmp", f"temp_image_{datetime.now().strftime('%Y%m%d_%H%M%S')}.jpg")
17
+ img.save(img_buffer, format="JPEG")
18
+
19
+ with open(img_buffer, "rb") as image_file:
20
+ return base64.b64encode(image_file.read()).decode("utf-8")
 
21
 
22
  # Function to generate product description using OpenAI API
23
  def generate_product_description(image, text_input=None):
24
+ # Encode the uploaded image
25
+ base64_image = encode_image(image)
26
 
27
+ headers = {
28
+ "Content-Type": "application/json",
29
+ "Authorization": f"Bearer {api_key}"
30
+ }
31
 
32
+ # Payload with base64 encoded image as a Data URL
33
+ payload = {
34
+ "model": "gpt-4o-mini",
35
+ "messages": [
36
  {
37
  "role": "user",
38
  "content": [
39
  {"type": "text", "text": text_input or "What's in this image?"},
40
  {
41
  "type": "image_url",
42
+ "image_url": {
43
+ "url": f"data:image/jpeg;base64,{base64_image}"
44
+ }
45
+ }
46
+ ]
47
  }
48
  ],
49
+ "max_tokens": 300
50
+ }
51
 
52
+ response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload)
53
+ response_data = response.json()
54
 
55
+ # Handle errors
56
+ if response.status_code != 200:
57
+ raise ValueError(f"OpenAI API Error: {response_data.get('error', {}).get('message', 'Unknown Error')}")
58
+
59
+ # Extract and return the generated message
60
+ return response_data["choices"][0]["message"]
61
 
62
  css = """
63
  #output {
 
82
  generate_product_description, [input_img, text_input], [output_text]
83
  )
84
 
85
+ # Launch Gradio app
86
  demo.queue(api_open=False)
87
+ demo.launch(debug=True)