Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,54 +1,43 @@
|
|
1 |
-
|
2 |
-
from transformers import MarianMTModel, MarianTokenizer
|
3 |
import gradio as gr
|
|
|
4 |
|
5 |
-
#
|
6 |
-
|
7 |
-
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
|
8 |
-
|
9 |
-
# Load MarianMT model for translation (English to Tamil)
|
10 |
-
translation_model_name = "Helsinki-NLP/opus-mt-en-ta"
|
11 |
-
translator_model = MarianMTModel.from_pretrained(translation_model_name)
|
12 |
-
translator_tokenizer = MarianTokenizer.from_pretrained(translation_model_name)
|
13 |
-
|
14 |
-
import numpy as np
|
15 |
-
from PIL import Image
|
16 |
-
import torch
|
17 |
|
18 |
def generate_caption(image):
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
interface.launch()
|
|
|
1 |
+
import openai
|
|
|
2 |
import gradio as gr
|
3 |
+
import os
|
4 |
|
5 |
+
# Set your OpenAI API Key
|
6 |
+
openai.api_key = "YOUR_OPENAI_API_KEY" # Replace with your API key
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
def generate_caption(image):
|
9 |
+
"""Generate a caption for the uploaded image using OpenAI's GPT-4 Vision API."""
|
10 |
+
with open(image, "rb") as img_file:
|
11 |
+
response = openai.ChatCompletion.create(
|
12 |
+
model="gpt-4-vision-preview",
|
13 |
+
messages=[
|
14 |
+
{
|
15 |
+
"role": "system",
|
16 |
+
"content": "You are an AI assistant that describes images accurately."
|
17 |
+
},
|
18 |
+
{
|
19 |
+
"role": "user",
|
20 |
+
"content": [
|
21 |
+
{"type": "text", "text": "Describe the contents of this image in detail."},
|
22 |
+
{"type": "image", "image": img_file.read()},
|
23 |
+
],
|
24 |
+
},
|
25 |
+
],
|
26 |
+
max_tokens=100
|
27 |
+
)
|
28 |
+
|
29 |
+
caption = response["choices"][0]["message"]["content"]
|
30 |
+
return caption
|
31 |
+
|
32 |
+
# Gradio UI
|
33 |
+
iface = gr.Interface(
|
34 |
+
fn=generate_caption,
|
35 |
+
inputs=gr.Image(type="filepath"),
|
36 |
+
outputs="text",
|
37 |
+
title="Image Captioning App",
|
38 |
+
description="Upload an image, and the AI will generate a descriptive caption."
|
39 |
+
)
|
40 |
+
|
41 |
+
# Run the app
|
42 |
+
if __name__ == "__main__":
|
43 |
+
iface.launch()
|
|