Tulitula commited on
Commit
ca8dd1a
·
verified ·
1 Parent(s): 7cef81c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -29
app.py CHANGED
@@ -1,15 +1,14 @@
 
 
1
  import re
2
  import gradio as gr
3
  from PIL import Image
4
- from transformers import BlipProcessor, BlipForConditionalGeneration, pipeline
5
 
6
- # Load BLIP for image captioning (slow processor, no torchvision dependency)
7
- blip_processor = BlipProcessor.from_pretrained(
8
- "Salesforce/blip-image-captioning-base",
9
- use_fast=False
10
- )
11
- blip_model = BlipForConditionalGeneration.from_pretrained(
12
- "Salesforce/blip-image-captioning-base"
13
  )
14
 
15
  # Helper to create Flan-T5 pipelines (temperature=1.0 for diversity)
@@ -24,12 +23,11 @@ def make_pipeline(model_name, max_tokens):
24
  )
25
 
26
  # Pipelines: category, analysis, suggestions
27
- category_generator = make_pipeline("google/flan-t5-small", 100)
28
- analysis_generator = make_pipeline("google/flan-t5-small", 500)
29
  suggestion_generator = make_pipeline("google/flan-t5-small", 500)
30
 
31
  # Example ads URLs for gallery
32
-
33
  def get_recommendations():
34
  return [
35
  "https://i.imgur.com/InC88PP.jpeg",
@@ -44,11 +42,10 @@ def get_recommendations():
44
  "https://i.imgur.com/Xj92Cjv.jpeg",
45
  ]
46
 
47
- # Step 1: BLIP generates a caption from the image
48
  def generate_caption(image):
49
- inputs = blip_processor(images=image, return_tensors="pt")
50
- outputs = blip_model.generate(**inputs)
51
- return blip_processor.decode(outputs[0], skip_special_tokens=True)
52
 
53
  # Step 2: Flan interprets caption into a concise category label
54
  def generate_category(caption):
@@ -74,36 +71,39 @@ def generate_suggestions(caption):
74
  "Each line must start with '- '."
75
  )
76
  raw = suggestion_generator(prompt)[0]["generated_text"].strip()
77
- lines = [line for line in raw.splitlines() if line.strip().startswith('- ')]
78
  if len(lines) < 5:
79
  all_lines = [l.strip() for l in raw.splitlines() if l.strip()]
80
- lines = [('- ' + all_lines[i]) if not all_lines[i].startswith('- ') else all_lines[i] for i in range(min(5, len(all_lines)))]
 
 
 
81
  return "\n".join(lines[:5])
82
 
83
- # Combine steps into one process
84
-
85
  def process(image):
86
- caption = generate_caption(image)
87
- category = generate_category(caption)
88
- analysis = generate_analysis(caption)
89
  suggestions = generate_suggestions(caption)
90
- recs = get_recommendations()
91
  return category, analysis, suggestions, recs
92
 
93
- # Gradio UI layout
94
  with gr.Blocks(theme=gr.themes.Default(primary_hue="blue")) as demo:
95
  gr.Markdown("## 📢 Smart Ad Analyzer")
96
  gr.Markdown(
97
- "Upload an image ad to see an Ad Category, a five-sentence Analysis, five bullet-point Suggestions, and Example Ads."
 
98
  )
99
 
100
  with gr.Row():
101
  image_input = gr.Image(type="pil", label="Upload Ad Image")
102
  with gr.Column():
103
- category_out = gr.Textbox(label="Ad Category", interactive=False)
104
- analysis_out = gr.Textbox(label="Ad Analysis", lines=5, interactive=False)
105
  suggestion_out = gr.Textbox(label="Improvement Suggestions", lines=5, interactive=False)
106
- btn = gr.Button("Analyze Ad", size="sm", variant="primary")
107
 
108
  recommendation_gallery = gr.Gallery(label="Recommended Example Ads", show_label=True)
109
 
@@ -116,4 +116,4 @@ with gr.Blocks(theme=gr.themes.Default(primary_hue="blue")) as demo:
116
  gr.Markdown("Made by Simon Thalmay")
117
 
118
  if __name__ == "__main__":
119
- demo.launch()
 
1
+ # app.py
2
+
3
  import re
4
  import gradio as gr
5
  from PIL import Image
6
+ from transformers import pipeline
7
 
8
+ # Use ChatDOC/OCRFlux-3B for image-to-text instead of BLIP
9
+ image_to_text = pipeline(
10
+ "image-to-text",
11
+ model="ChatDOC/OCRFlux-3B"
 
 
 
12
  )
13
 
14
  # Helper to create Flan-T5 pipelines (temperature=1.0 for diversity)
 
23
  )
24
 
25
  # Pipelines: category, analysis, suggestions
26
+ category_generator = make_pipeline("google/flan-t5-small", 100)
27
+ analysis_generator = make_pipeline("google/flan-t5-small", 500)
28
  suggestion_generator = make_pipeline("google/flan-t5-small", 500)
29
 
30
  # Example ads URLs for gallery
 
31
  def get_recommendations():
32
  return [
33
  "https://i.imgur.com/InC88PP.jpeg",
 
42
  "https://i.imgur.com/Xj92Cjv.jpeg",
43
  ]
44
 
45
+ # Step 1: Use OCRFlux to get a detailed textual description of the image
46
  def generate_caption(image):
47
+ result = image_to_text(image)
48
+ return result[0]["generated_text"].strip()
 
49
 
50
  # Step 2: Flan interprets caption into a concise category label
51
  def generate_category(caption):
 
71
  "Each line must start with '- '."
72
  )
73
  raw = suggestion_generator(prompt)[0]["generated_text"].strip()
74
+ lines = [l for l in raw.splitlines() if l.strip().startswith('- ')]
75
  if len(lines) < 5:
76
  all_lines = [l.strip() for l in raw.splitlines() if l.strip()]
77
+ lines = [
78
+ ('- ' + all_lines[i]) if not all_lines[i].startswith('- ') else all_lines[i]
79
+ for i in range(min(5, len(all_lines)))
80
+ ]
81
  return "\n".join(lines[:5])
82
 
83
+ # Full workflow
 
84
  def process(image):
85
+ caption = generate_caption(image)
86
+ category = generate_category(caption)
87
+ analysis = generate_analysis(caption)
88
  suggestions = generate_suggestions(caption)
89
+ recs = get_recommendations()
90
  return category, analysis, suggestions, recs
91
 
92
+ # Gradio UI
93
  with gr.Blocks(theme=gr.themes.Default(primary_hue="blue")) as demo:
94
  gr.Markdown("## 📢 Smart Ad Analyzer")
95
  gr.Markdown(
96
+ "Upload an image ad to see an Ad Category, a five-sentence Analysis, "
97
+ "five bullet-point Suggestions, and Example Ads."
98
  )
99
 
100
  with gr.Row():
101
  image_input = gr.Image(type="pil", label="Upload Ad Image")
102
  with gr.Column():
103
+ category_out = gr.Textbox(label="Ad Category", interactive=False)
104
+ analysis_out = gr.Textbox(label="Ad Analysis", lines=5, interactive=False)
105
  suggestion_out = gr.Textbox(label="Improvement Suggestions", lines=5, interactive=False)
106
+ btn = gr.Button("Analyze Ad", size="sm", variant="primary")
107
 
108
  recommendation_gallery = gr.Gallery(label="Recommended Example Ads", show_label=True)
109
 
 
116
  gr.Markdown("Made by Simon Thalmay")
117
 
118
  if __name__ == "__main__":
119
+ demo.launch()