Update app.py
Browse files
app.py
CHANGED
|
@@ -1,45 +1,48 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from PIL import Image
|
| 3 |
-
import torch
|
| 4 |
-
from transformers import CLIPProcessor, CLIPModel
|
| 5 |
-
import openai
|
| 6 |
import requests
|
| 7 |
import base64
|
| 8 |
import io
|
| 9 |
import os
|
| 10 |
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
clip_model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32")
|
| 13 |
clip_processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")
|
| 14 |
|
| 15 |
-
# Load API keys from environment variables
|
| 16 |
-
openai.api_key = os.getenv("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")
|
| 17 |
-
ocr_api_key = os.getenv("K86540556188957")
|
| 18 |
|
| 19 |
-
# Function to perform OCR using OCR.Space
|
| 20 |
def extract_text_with_ocr_space(image):
|
| 21 |
buffered = io.BytesIO()
|
| 22 |
image.save(buffered, format="JPEG")
|
| 23 |
img_str = base64.b64encode(buffered.getvalue()).decode()
|
| 24 |
|
|
|
|
|
|
|
|
|
|
| 25 |
response = requests.post(
|
| 26 |
"https://api.ocr.space/parse/image",
|
| 27 |
data={
|
| 28 |
"base64Image": f"data:image/jpeg;base64,{img_str}",
|
| 29 |
"language": "eng",
|
| 30 |
"isOverlayRequired": False,
|
| 31 |
-
"apikey":
|
| 32 |
}
|
| 33 |
)
|
| 34 |
result = response.json()
|
| 35 |
return result['ParsedResults'][0]['ParsedText'] if "ParsedResults" in result else ""
|
| 36 |
|
| 37 |
-
|
| 38 |
def generate_alt_text(image):
|
| 39 |
# Step 1: OCR
|
| 40 |
ocr_text = extract_text_with_ocr_space(image)
|
| 41 |
|
| 42 |
-
# Step 2:
|
| 43 |
candidate_descriptions = [
|
| 44 |
"a manga cover with a male character",
|
| 45 |
"a man wearing a summer floral shirt",
|
|
@@ -55,7 +58,7 @@ def generate_alt_text(image):
|
|
| 55 |
probs = outputs.logits_per_image.softmax(dim=1)
|
| 56 |
sorted_descriptions = sorted(zip(candidate_descriptions, probs[0].tolist()), key=lambda x: x[1], reverse=True)
|
| 57 |
|
| 58 |
-
# Step 3: Prompt
|
| 59 |
top_descriptions = "\n".join([f"- {desc}" for desc, _ in sorted_descriptions[:5]])
|
| 60 |
prompt = f"""
|
| 61 |
Generate a detailed and professional alt text for a manga or anime image using the following extracted text and visual description.
|
|
@@ -69,20 +72,21 @@ Visual Description:
|
|
| 69 |
Alt Text:
|
| 70 |
"""
|
| 71 |
|
| 72 |
-
# Step 4:
|
| 73 |
-
response =
|
| 74 |
model="gpt-4",
|
| 75 |
messages=[{"role": "user", "content": prompt}],
|
| 76 |
temperature=0.4
|
| 77 |
)
|
| 78 |
|
| 79 |
-
return response
|
|
|
|
| 80 |
|
| 81 |
-
# Gradio
|
| 82 |
gr.Interface(
|
| 83 |
fn=generate_alt_text,
|
| 84 |
inputs=gr.Image(type="pil"),
|
| 85 |
outputs=gr.Textbox(label="Generated Alt Text"),
|
| 86 |
-
title="
|
| 87 |
-
description="Upload a manga or anime-style image to generate descriptive alt text using OCR, CLIP, and GPT"
|
| 88 |
).launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from PIL import Image
|
|
|
|
|
|
|
|
|
|
| 3 |
import requests
|
| 4 |
import base64
|
| 5 |
import io
|
| 6 |
import os
|
| 7 |
|
| 8 |
+
import torch
|
| 9 |
+
from transformers import CLIPProcessor, CLIPModel
|
| 10 |
+
from openai import OpenAI
|
| 11 |
+
|
| 12 |
+
# Load OpenAI client
|
| 13 |
+
client = OpenAI(api_key=os.getenv("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"))
|
| 14 |
+
|
| 15 |
+
# Load CLIP model from Hugging Face
|
| 16 |
clip_model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32")
|
| 17 |
clip_processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")
|
| 18 |
|
|
|
|
|
|
|
|
|
|
| 19 |
|
|
|
|
| 20 |
def extract_text_with_ocr_space(image):
|
| 21 |
buffered = io.BytesIO()
|
| 22 |
image.save(buffered, format="JPEG")
|
| 23 |
img_str = base64.b64encode(buffered.getvalue()).decode()
|
| 24 |
|
| 25 |
+
# Use your OCR.Space API key from Hugging Face Secrets
|
| 26 |
+
api_key = os.getenv("K86540556188957")
|
| 27 |
+
|
| 28 |
response = requests.post(
|
| 29 |
"https://api.ocr.space/parse/image",
|
| 30 |
data={
|
| 31 |
"base64Image": f"data:image/jpeg;base64,{img_str}",
|
| 32 |
"language": "eng",
|
| 33 |
"isOverlayRequired": False,
|
| 34 |
+
"apikey": api_key
|
| 35 |
}
|
| 36 |
)
|
| 37 |
result = response.json()
|
| 38 |
return result['ParsedResults'][0]['ParsedText'] if "ParsedResults" in result else ""
|
| 39 |
|
| 40 |
+
|
| 41 |
def generate_alt_text(image):
|
| 42 |
# Step 1: OCR
|
| 43 |
ocr_text = extract_text_with_ocr_space(image)
|
| 44 |
|
| 45 |
+
# Step 2: CLIP visual descriptions
|
| 46 |
candidate_descriptions = [
|
| 47 |
"a manga cover with a male character",
|
| 48 |
"a man wearing a summer floral shirt",
|
|
|
|
| 58 |
probs = outputs.logits_per_image.softmax(dim=1)
|
| 59 |
sorted_descriptions = sorted(zip(candidate_descriptions, probs[0].tolist()), key=lambda x: x[1], reverse=True)
|
| 60 |
|
| 61 |
+
# Step 3: Prompt construction
|
| 62 |
top_descriptions = "\n".join([f"- {desc}" for desc, _ in sorted_descriptions[:5]])
|
| 63 |
prompt = f"""
|
| 64 |
Generate a detailed and professional alt text for a manga or anime image using the following extracted text and visual description.
|
|
|
|
| 72 |
Alt Text:
|
| 73 |
"""
|
| 74 |
|
| 75 |
+
# Step 4: GPT generation
|
| 76 |
+
response = client.chat.completions.create(
|
| 77 |
model="gpt-4",
|
| 78 |
messages=[{"role": "user", "content": prompt}],
|
| 79 |
temperature=0.4
|
| 80 |
)
|
| 81 |
|
| 82 |
+
return response.choices[0].message.content
|
| 83 |
+
|
| 84 |
|
| 85 |
+
# Gradio app interface
|
| 86 |
gr.Interface(
|
| 87 |
fn=generate_alt_text,
|
| 88 |
inputs=gr.Image(type="pil"),
|
| 89 |
outputs=gr.Textbox(label="Generated Alt Text"),
|
| 90 |
+
title="Anime/Manga Image Alt Text Generator",
|
| 91 |
+
description="Upload a manga or anime-style image to generate a descriptive alt text using OCR, CLIP, and GPT"
|
| 92 |
).launch()
|