apratim24 commited on
Commit
4071fde
·
verified ·
1 Parent(s): c08aca3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -52
app.py CHANGED
@@ -1,7 +1,3 @@
1
- import gradio as gr
2
-
3
- # Using openai models ---------------------------------------------------------
4
-
5
  from langchain_openai import OpenAI
6
  import os
7
  openai_api_key = os.getenv("OPENAI_API_KEY")
@@ -11,58 +7,60 @@ import base64
11
  import requests
12
  from PIL import Image
13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  def generate_story(image, theme, genre, word_count):
15
  try:
16
 
17
- width = 1000
18
-
19
- # Function to resize image maintaining aspect ratio with a maximum width of 1000 pixels
20
- def resize_image(image, max_width=width):
21
- with Image.open(image) as img:
22
- ratio = max_width / img.width
23
- new_height = int(img.height * ratio)
24
- resized_img = img.resize((max_width, new_height), Image.ANTIALIAS)
25
- img_byte_arr = io.BytesIO()
26
- resized_img.save(img_byte_arr, format=img.format)
27
- return img_byte_arr.getvalue()
28
-
29
- # Function to encode the image to base64
30
- def encode_image(image):
31
- resized_image_bytes = resize_image(image) # Resize the image
32
- return base64.b64encode(resized_image_bytes).decode('utf-8')
33
 
34
- # Function to call the API for image and get the response
35
- def get_response_for_image(openai_api_key, image):
36
- base64_image = encode_image(image)
37
- headers = {
38
- "Content-Type": "application/json",
39
- "Authorization": f"Bearer {openai_api_key}"
40
- }
41
- payload = {
42
- "model": "gpt-4o",
43
- "messages": [
44
- {
45
- "role": "user",
46
- "content": [
47
- {
48
- "type": "text",
49
- "text": '''Describe or caption the image within 20 words.'''
50
- },
51
- {
52
- "type": "image_url",
53
- "image_url": {
54
- "url": f"data:image/jpeg;base64,{base64_image}",
55
- "detail": "low"
56
- }
57
- }
58
- ]
59
- }
60
- ],
61
- "max_tokens": 500
62
- }
63
- response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload)
64
- return response['choices'][0]['message']['content']
65
-
66
 
67
  # Decode the caption
68
  caption_text = get_response_for_image(openai_api_key, image)
@@ -139,3 +137,4 @@ gr.Interface(
139
  title="Image to Story Generator",
140
  description="Generate a story from an image taking theme and genre as input. It leverages image captioning and text generation models.",
141
  ).launch()
 
 
 
 
 
 
1
  from langchain_openai import OpenAI
2
  import os
3
  openai_api_key = os.getenv("OPENAI_API_KEY")
 
7
  import requests
8
  from PIL import Image
9
 
10
+ width = 1000
11
+
12
+ # Function to resize image maintaining aspect ratio with a maximum width of 1000 pixels
13
+ def resize_image(image, max_width=width):
14
+ with Image.open(image) as img:
15
+ ratio = max_width / img.width
16
+ new_height = int(img.height * ratio)
17
+ resized_img = img.resize((max_width, new_height), Image.ANTIALIAS)
18
+ img_byte_arr = io.BytesIO()
19
+ resized_img.save(img_byte_arr, format=img.format)
20
+ return img_byte_arr.getvalue()
21
+
22
+ # Function to encode the image to base64
23
+ def encode_image(image):
24
+ resized_image_bytes = resize_image(image) # Resize the image
25
+ return base64.b64encode(resized_image_bytes).decode('utf-8')
26
+
27
+ # Function to call the API for image and get the response
28
+ def get_response_for_image(openai_api_key, image):
29
+ base64_image = encode_image(image)
30
+ headers = {
31
+ "Content-Type": "application/json",
32
+ "Authorization": f"Bearer {openai_api_key}"
33
+ }
34
+ payload = {
35
+ "model": "gpt-4o",
36
+ "messages": [
37
+ {
38
+ "role": "user",
39
+ "content": [
40
+ {
41
+ "type": "text",
42
+ "text": '''Describe or caption the image within 20 words.'''
43
+ },
44
+ {
45
+ "type": "image_url",
46
+ "image_url": {
47
+ "url": f"data:image/jpeg;base64,{base64_image}",
48
+ "detail": "low"
49
+ }
50
+ }
51
+ ]
52
+ }
53
+ ],
54
+ "max_tokens": 500
55
+ }
56
+ response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload)
57
+ return response['choices'][0]['message']['content']
58
+
59
+
60
  def generate_story(image, theme, genre, word_count):
61
  try:
62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
 
65
  # Decode the caption
66
  caption_text = get_response_for_image(openai_api_key, image)
 
137
  title="Image to Story Generator",
138
  description="Generate a story from an image taking theme and genre as input. It leverages image captioning and text generation models.",
139
  ).launch()
140
+