Tulitula commited on
Commit
8c5c487
·
verified ·
1 Parent(s): 75ba702

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -20
app.py CHANGED
@@ -1,17 +1,15 @@
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)
15
  def make_pipeline(model_name, max_tokens):
16
  return pipeline(
17
  "text2text-generation",
@@ -19,15 +17,18 @@ def make_pipeline(model_name, max_tokens):
19
  tokenizer=model_name,
20
  max_new_tokens=max_tokens,
21
  do_sample=True,
22
- temperature=1.0
 
 
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,18 +43,22 @@ def get_recommendations():
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):
52
  prompt = f"Caption: {caption}\nProvide a concise category label for this ad."
53
  raw = category_generator(prompt)[0]["generated_text"].strip()
54
  return raw.splitlines()[0]
55
 
56
- # Step 3: Flan writes exactly five sentences of analysis
 
57
  def generate_analysis(caption):
58
  prompt = (
59
  f"Caption: {caption}\n"
@@ -63,7 +68,8 @@ def generate_analysis(caption):
63
  sentences = re.split(r'(?<=[.!?])\s+', raw)
64
  return " ".join(sentences[:5])
65
 
66
- # Step 4: Flan suggests five bullet-point improvements
 
67
  def generate_suggestions(caption):
68
  prompt = (
69
  f"Caption: {caption}\n"
@@ -74,13 +80,12 @@ def generate_suggestions(caption):
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)
@@ -89,11 +94,11 @@ def process(image):
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
 
@@ -116,4 +121,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
  import re
2
  import gradio as gr
3
  from PIL import Image
4
  from transformers import pipeline
5
 
6
+ # 1) Image-to-text: ChatDOC/OCRFlux-3B for rich description
7
  image_to_text = pipeline(
8
  "image-to-text",
9
  model="ChatDOC/OCRFlux-3B"
10
  )
11
 
12
+ # 2) Helper to build Flan-T5-small text pipelines (temp=1.0)
13
  def make_pipeline(model_name, max_tokens):
14
  return pipeline(
15
  "text2text-generation",
 
17
  tokenizer=model_name,
18
  max_new_tokens=max_tokens,
19
  do_sample=True,
20
+ temperature=1.0,
21
+ top_k=50,
22
+ top_p=0.95
23
  )
24
 
25
+ # 3) 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
+ # Hardcoded example ads for gallery
31
+
32
  def get_recommendations():
33
  return [
34
  "https://i.imgur.com/InC88PP.jpeg",
 
43
  "https://i.imgur.com/Xj92Cjv.jpeg",
44
  ]
45
 
46
+ # Step A: Use OCRFlux to generate a detailed caption
47
+
48
  def generate_caption(image):
49
  result = image_to_text(image)
50
+ text = result[0]["generated_text"].strip()
51
+ return text
52
+
53
+ # Step B: Flan interprets caption into concise category
54
 
 
55
  def generate_category(caption):
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
+
62
  def generate_analysis(caption):
63
  prompt = (
64
  f"Caption: {caption}\n"
 
68
  sentences = re.split(r'(?<=[.!?])\s+', raw)
69
  return " ".join(sentences[:5])
70
 
71
+ # Step D: Flan suggests five actionable bullet-point improvements
72
+
73
  def generate_suggestions(caption):
74
  prompt = (
75
  f"Caption: {caption}\n"
 
80
  lines = [l for l in raw.splitlines() if l.strip().startswith('- ')]
81
  if len(lines) < 5:
82
  all_lines = [l.strip() for l in raw.splitlines() if l.strip()]
83
+ lines = [('- ' + all_lines[i]) if not all_lines[i].startswith('- ') else all_lines[i]
84
+ for i in range(min(5, len(all_lines)))]
 
 
85
  return "\n".join(lines[:5])
86
 
87
+ # Orchestrator: process image through all steps
88
+
89
  def process(image):
90
  caption = generate_caption(image)
91
  category = generate_category(caption)
 
94
  recs = get_recommendations()
95
  return category, analysis, suggestions, recs
96
 
97
+ # Gradio UI layout
98
  with gr.Blocks(theme=gr.themes.Default(primary_hue="blue")) as demo:
99
  gr.Markdown("## 📢 Smart Ad Analyzer")
100
  gr.Markdown(
101
+ "Upload an image ad to see: an Ad Category, a five-sentence Analysis, "
102
  "five bullet-point Suggestions, and Example Ads."
103
  )
104
 
 
121
  gr.Markdown("Made by Simon Thalmay")
122
 
123
  if __name__ == "__main__":
124
+ demo.launch()