Spaces:
Sleeping
Sleeping
Suzen Fylke
commited on
Commit
•
3a57c8d
1
Parent(s):
dd81f62
Add application files
Browse files- .gitignore +1 -0
- README.md +1 -3
- app.py +58 -0
- requirements.txt +5 -0
.gitignore
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
.python-version
|
README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
---
|
2 |
title: Dystopedia
|
3 |
-
emoji:
|
4 |
colorFrom: green
|
5 |
colorTo: purple
|
6 |
sdk: gradio
|
@@ -8,5 +8,3 @@ sdk_version: 3.1.6
|
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
---
|
11 |
-
|
12 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
1 |
---
|
2 |
title: Dystopedia
|
3 |
+
emoji: 🫠
|
4 |
colorFrom: green
|
5 |
colorTo: purple
|
6 |
sdk: gradio
|
|
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
---
|
|
|
|
app.py
ADDED
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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(
|
9 |
+
"sentiment-analysis",
|
10 |
+
model="distilbert-base-uncased-finetuned-sst-2-english",
|
11 |
+
revision="af0f99b"
|
12 |
+
)
|
13 |
+
|
14 |
+
def is_positive(text):
|
15 |
+
return sentiment_analyzer(text)[0]["label"] == "POSITIVE"
|
16 |
+
|
17 |
+
def make_past_tense(token):
|
18 |
+
if token.tag_ in ("VBP", "VBZ"):
|
19 |
+
return f'{token._.inflect("VBD")} '
|
20 |
+
return token.text_with_ws
|
21 |
+
|
22 |
+
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. "
|
44 |
+
"Inspired by [this Tweet](https://twitter.com/lbcyber/status/1115015586243862528)"
|
45 |
+
)
|
46 |
+
examples = ["joy", "hope", "peace", "Earth", "water", "food"]
|
47 |
+
gr.Interface(
|
48 |
+
fn=get_summary,
|
49 |
+
inputs=gr.Textbox(label="term", placeholder="Enter a term...", max_lines=1),
|
50 |
+
outputs=gr.Textbox(label="description"),
|
51 |
+
title=title,
|
52 |
+
description=description,
|
53 |
+
examples=examples,
|
54 |
+
cache_examples=True,
|
55 |
+
allow_flagging="never",
|
56 |
+
).launch()
|
57 |
+
|
58 |
+
launch_demo()
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
lemminflect
|
2 |
+
gradio
|
3 |
+
spacy
|
4 |
+
transformers
|
5 |
+
wikipedia
|