Multiverse26 commited on
Commit
bd347e1
·
verified ·
1 Parent(s): c141be1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -5
app.py CHANGED
@@ -1,5 +1,6 @@
1
  import gradio as gr
2
  from transformers import pipeline
 
3
 
4
  generator = pipeline("text-generation", model="gpt2")
5
 
@@ -13,22 +14,33 @@ def generate_caption(prompt):
13
  temperature=0.9,
14
  top_k=50,
15
  top_p=0.95,
16
- repetition_penalty=1.2
17
  )
18
 
19
  captions = []
20
 
21
  for r in result:
22
  text = r["generated_text"].replace(base_prompt, "").strip()
23
- line = text.split("\n")[0].strip()
24
 
25
- # avoid duplicates
26
- if line and line not in captions:
27
- captions.append(line)
 
 
 
 
28
 
29
  if len(captions) == 3:
30
  break
31
 
 
 
 
 
 
 
 
 
32
  return "\n".join(captions)
33
 
34
  iface = gr.Interface(
 
1
  import gradio as gr
2
  from transformers import pipeline
3
+ import re
4
 
5
  generator = pipeline("text-generation", model="gpt2")
6
 
 
14
  temperature=0.9,
15
  top_k=50,
16
  top_p=0.95,
17
+ repetition_penalty=1.3
18
  )
19
 
20
  captions = []
21
 
22
  for r in result:
23
  text = r["generated_text"].replace(base_prompt, "").strip()
 
24
 
25
+ # Take only first sentence
26
+ sentence = re.split(r'[.!?]', text)[0].strip()
27
+
28
+ # Clean unwanted long garbage
29
+ if len(sentence) > 5 and len(sentence) < 80:
30
+ if sentence not in captions:
31
+ captions.append(sentence)
32
 
33
  if len(captions) == 3:
34
  break
35
 
36
+ # fallback if nothing good
37
+ if not captions:
38
+ captions = [
39
+ "Elegant shine for every moment ✨",
40
+ "Where luxury meets simplicity",
41
+ "A touch of gold, a touch of power"
42
+ ]
43
+
44
  return "\n".join(captions)
45
 
46
  iface = gr.Interface(