Suzen Fylke commited on
Commit
be91caa
1 Parent(s): ae967d5

Use gr.Error

Browse files
Files changed (1) hide show
  1. app.py +28 -16
app.py CHANGED
@@ -1,8 +1,8 @@
1
  import gradio as gr
2
  import lemminflect
3
  import spacy
4
- from transformers import pipeline
5
  import wikipedia
 
6
 
7
  nlp = spacy.load("en_core_web_lg")
8
  sentiment_analyzer = pipeline(
@@ -23,30 +23,42 @@ def make_dystopian(term, text):
23
  doc = nlp(text)
24
  if is_positive(term):
25
  return "".join([make_past_tense(token) for token in doc])
26
- return doc.text_with_ws
 
 
 
 
27
 
28
- def get_summary(term):
29
- if not term:
30
- return ""
31
  try:
32
- results = wikipedia.search(term)
33
  except wikipedia.exceptions.DisambiguationError as e:
34
- return e.error
35
- if len(results) > 0:
36
- summary = wikipedia.summary(results[0], sentences=1, auto_suggest=False, redirect=True)
37
- return make_dystopian(term, summary)
38
- return "Could not find an article on the term provided."
 
 
 
 
 
 
 
 
 
 
39
 
40
- def launch_demo():
41
  title = "Dystopedia"
42
  description = (
43
- "Make any Wikipedia topic dystopian. Inspired by [this Tweet](https://twitter.com/lbcyber/status/1115015586243862528). "
 
44
  "Dystopedia uses [DistilBERT base uncased finetuned SST-2](https://huggingface.co/distilbert-base-uncased-finetuned-sst-2-english) "
45
  "for sentiment analysis and is subject to its limitations and biases."
46
  )
47
  examples = ["joy", "hope", "peace", "Earth", "water", "food"]
48
  gr.Interface(
49
- fn=get_summary,
50
  inputs=gr.Textbox(label="term", placeholder="Enter a term...", max_lines=1),
51
  outputs=gr.Textbox(label="description"),
52
  title=title,
@@ -54,6 +66,6 @@ def launch_demo():
54
  examples=examples,
55
  cache_examples=True,
56
  allow_flagging="never",
57
- ).launch()
58
 
59
- launch_demo()
 
1
  import gradio as gr
2
  import lemminflect
3
  import spacy
 
4
  import wikipedia
5
+ from transformers import pipeline
6
 
7
  nlp = spacy.load("en_core_web_lg")
8
  sentiment_analyzer = pipeline(
 
23
  doc = nlp(text)
24
  if is_positive(term):
25
  return "".join([make_past_tense(token) for token in doc])
26
+ return doc.text
27
+
28
+ def get_dystopian_summary(term):
29
+ if term == "":
30
+ return term
31
 
 
 
 
32
  try:
33
+ results = wikipedia.search(term, results=1)
34
  except wikipedia.exceptions.DisambiguationError as e:
35
+ raise gr.Error(e.error)
36
+
37
+ if len(results) == 0:
38
+ raise gr.Error(
39
+ f'Could not find an article on the term "{term}". '
40
+ 'Try searching for a different topic.'
41
+ )
42
+
43
+ summary = wikipedia.summary(
44
+ results[0],
45
+ sentences=1,
46
+ auto_suggest=False,
47
+ redirect=True
48
+ )
49
+ return make_dystopian(term, summary)
50
 
51
+ def launch_demo(**kwargs):
52
  title = "Dystopedia"
53
  description = (
54
+ "Make any Wikipedia topic dystopian. Inspired by "
55
+ "[this Tweet](https://twitter.com/lbcyber/status/1115015586243862528). "
56
  "Dystopedia uses [DistilBERT base uncased finetuned SST-2](https://huggingface.co/distilbert-base-uncased-finetuned-sst-2-english) "
57
  "for sentiment analysis and is subject to its limitations and biases."
58
  )
59
  examples = ["joy", "hope", "peace", "Earth", "water", "food"]
60
  gr.Interface(
61
+ fn=get_dystopian_summary,
62
  inputs=gr.Textbox(label="term", placeholder="Enter a term...", max_lines=1),
63
  outputs=gr.Textbox(label="description"),
64
  title=title,
 
66
  examples=examples,
67
  cache_examples=True,
68
  allow_flagging="never",
69
+ ).launch(**kwargs)
70
 
71
+ launch_demo(show_error=True)