Ujeshhh commited on
Commit
55f0a1e
·
verified ·
1 Parent(s): 6579c7d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -29
app.py CHANGED
@@ -1,36 +1,25 @@
1
- import openai
2
  import gradio as gr
3
- import os
4
- import base64
5
 
6
- # Set your OpenAI API Key
7
- openai.api_key = "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" # Replace with your API key
8
 
9
  def generate_caption(image):
10
- """Generate a caption for the uploaded image using OpenAI's GPT-4 Vision API."""
11
-
12
- # Convert image to Base64
13
- with open(image, "rb") as img_file:
14
- img_base64 = base64.b64encode(img_file.read()).decode("utf-8")
15
 
16
- # OpenAI API call
17
- response = openai.chat.completions.create(
18
- model="gpt-4o-mini",
19
- messages=[
20
- {"role": "system", "content": "You are an AI that describes images accurately."},
21
- {
22
- "role": "user",
23
- "content": [
24
- {"type": "text", "text": "Describe this image in detail."},
25
- {"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{img_base64}"}},
26
- ],
27
- },
28
- ],
29
- max_tokens=100
30
- )
31
 
32
- # Extract and return caption
33
- caption = response.choices[0].message.content
 
 
 
 
 
34
  return caption
35
 
36
  # Gradio UI
@@ -38,8 +27,8 @@ iface = gr.Interface(
38
  fn=generate_caption,
39
  inputs=gr.Image(type="filepath"),
40
  outputs="text",
41
- title="Image Captioning App",
42
- description="Upload an image, and the AI will generate a descriptive caption."
43
  )
44
 
45
  # Run the app
 
1
+ import google.generativeai as genai
2
  import gradio as gr
3
+ from PIL import Image
 
4
 
5
+ # Set up Gemini API key
6
+ genai.configure(api_key="AIzaSyDnx_qUjGTFG1pv1otPUhNt_bGGv14aMDI") # Replace with your API key
7
 
8
  def generate_caption(image):
9
+ """Generate a caption for the uploaded image using Google Gemini Pro Vision API."""
 
 
 
 
10
 
11
+ model = genai.GenerativeModel("gemini-pro-vision")
12
+
13
+ # Open image using PIL
14
+ img = Image.open(image)
 
 
 
 
 
 
 
 
 
 
 
15
 
16
+ # Generate caption
17
+ response = model.generate_content(
18
+ [img, "Describe the contents of this image in detail."]
19
+ )
20
+
21
+ # Extract caption
22
+ caption = response.text
23
  return caption
24
 
25
  # Gradio UI
 
27
  fn=generate_caption,
28
  inputs=gr.Image(type="filepath"),
29
  outputs="text",
30
+ title="Free Image Captioning App",
31
+ description="Upload an image, and the AI (Google Gemini) will generate a descriptive caption."
32
  )
33
 
34
  # Run the app