Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,15 +1,25 @@
|
|
|
|
|
|
|
|
| 1 |
import re
|
| 2 |
import gradio as gr
|
| 3 |
-
from
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
model="ChatDOC/OCRFlux-3B"
|
| 9 |
)
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
# 2) Helper to build Flan-T5-small text pipelines (temperature=1.0)
|
| 12 |
-
def make_pipeline(model_name, max_tokens):
|
| 13 |
return pipeline(
|
| 14 |
"text2text-generation",
|
| 15 |
model=model_name,
|
|
@@ -41,19 +51,14 @@ def get_recommendations():
|
|
| 41 |
"https://i.imgur.com/Xj92Cjv.jpeg",
|
| 42 |
]
|
| 43 |
|
| 44 |
-
# Step A: Use OCRFlux to generate a detailed caption
|
| 45 |
-
def generate_caption(image):
|
| 46 |
-
result = image_to_text(image)
|
| 47 |
-
return result[0]["generated_text"].strip()
|
| 48 |
-
|
| 49 |
# Step B: Flan interprets caption into concise category
|
| 50 |
-
def generate_category(caption):
|
| 51 |
prompt = f"Caption: {caption}\nProvide a concise category label for this ad."
|
| 52 |
raw = category_generator(prompt)[0]["generated_text"].strip()
|
| 53 |
return raw.splitlines()[0]
|
| 54 |
|
| 55 |
# Step C: Flan produces exactly five-sentence analysis
|
| 56 |
-
def generate_analysis(caption):
|
| 57 |
prompt = (
|
| 58 |
f"Caption: {caption}\n"
|
| 59 |
"In exactly five sentences, explain what this ad communicates and its emotional impact."
|
|
@@ -63,7 +68,7 @@ def generate_analysis(caption):
|
|
| 63 |
return " ".join(sentences[:5])
|
| 64 |
|
| 65 |
# Step D: Flan suggests five actionable bullet-point improvements
|
| 66 |
-
def generate_suggestions(caption):
|
| 67 |
prompt = (
|
| 68 |
f"Caption: {caption}\n"
|
| 69 |
"Suggest five distinct improvements as bullet points. Each line must start with '- '."
|
|
@@ -79,7 +84,7 @@ def generate_suggestions(caption):
|
|
| 79 |
return "\n".join(lines[:5])
|
| 80 |
|
| 81 |
# Orchestrator: process image through all steps
|
| 82 |
-
def process(image):
|
| 83 |
caption = generate_caption(image)
|
| 84 |
category = generate_category(caption)
|
| 85 |
analysis = generate_analysis(caption)
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
|
| 3 |
import re
|
| 4 |
import gradio as gr
|
| 5 |
+
from PIL import Image
|
| 6 |
+
from transformers import (
|
| 7 |
+
Blip2Processor,
|
| 8 |
+
Blip2ForConditionalGeneration,
|
| 9 |
+
pipeline
|
|
|
|
| 10 |
)
|
| 11 |
|
| 12 |
+
# 1) BLIP-2 for richer image captions
|
| 13 |
+
processor = Blip2Processor.from_pretrained("Salesforce/blip2-opt-2.7b")
|
| 14 |
+
model = Blip2ForConditionalGeneration.from_pretrained("Salesforce/blip2-opt-2.7b")
|
| 15 |
+
|
| 16 |
+
def generate_caption(image: Image) -> str:
|
| 17 |
+
inputs = processor(images=image, return_tensors="pt")
|
| 18 |
+
outputs = model.generate(**inputs)
|
| 19 |
+
return processor.decode(outputs[0], skip_special_tokens=True)
|
| 20 |
+
|
| 21 |
# 2) Helper to build Flan-T5-small text pipelines (temperature=1.0)
|
| 22 |
+
def make_pipeline(model_name: str, max_tokens: int):
|
| 23 |
return pipeline(
|
| 24 |
"text2text-generation",
|
| 25 |
model=model_name,
|
|
|
|
| 51 |
"https://i.imgur.com/Xj92Cjv.jpeg",
|
| 52 |
]
|
| 53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
# Step B: Flan interprets caption into concise category
|
| 55 |
+
def generate_category(caption: str) -> str:
|
| 56 |
prompt = f"Caption: {caption}\nProvide a concise category label for this ad."
|
| 57 |
raw = category_generator(prompt)[0]["generated_text"].strip()
|
| 58 |
return raw.splitlines()[0]
|
| 59 |
|
| 60 |
# Step C: Flan produces exactly five-sentence analysis
|
| 61 |
+
def generate_analysis(caption: str) -> str:
|
| 62 |
prompt = (
|
| 63 |
f"Caption: {caption}\n"
|
| 64 |
"In exactly five sentences, explain what this ad communicates and its emotional impact."
|
|
|
|
| 68 |
return " ".join(sentences[:5])
|
| 69 |
|
| 70 |
# Step D: Flan suggests five actionable bullet-point improvements
|
| 71 |
+
def generate_suggestions(caption: str) -> str:
|
| 72 |
prompt = (
|
| 73 |
f"Caption: {caption}\n"
|
| 74 |
"Suggest five distinct improvements as bullet points. Each line must start with '- '."
|
|
|
|
| 84 |
return "\n".join(lines[:5])
|
| 85 |
|
| 86 |
# Orchestrator: process image through all steps
|
| 87 |
+
def process(image: Image):
|
| 88 |
caption = generate_caption(image)
|
| 89 |
category = generate_category(caption)
|
| 90 |
analysis = generate_analysis(caption)
|