Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,47 +1,46 @@
|
|
| 1 |
-
# app.py
|
| 2 |
-
import os, random
|
| 3 |
-
from collections import defaultdict
|
| 4 |
-
import gradio as gr
|
| 5 |
-
|
| 6 |
-
#
|
| 7 |
-
HERE = os.path.dirname(__file__)
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
state
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
output
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
gr.
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
if __name__ == "__main__":
|
| 47 |
demo.launch(share=True)
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
import os, random
|
| 3 |
+
from collections import defaultdict
|
| 4 |
+
import gradio as gr
|
| 5 |
+
|
| 6 |
+
# 1) Load & tokenize the corpus
|
| 7 |
+
HERE = os.path.dirname(__file__)
|
| 8 |
+
CORPUS_FILE = os.path.join(HERE, "corpus_earth_10k.txt")
|
| 9 |
+
|
| 10 |
+
with open(CORPUS_FILE, "r", encoding="utf-8") as f:
|
| 11 |
+
tokens = f.read().split()
|
| 12 |
+
|
| 13 |
+
# 2) Building Markov chain with order = 2
|
| 14 |
+
k = 2
|
| 15 |
+
chain = defaultdict(list)
|
| 16 |
+
for i in range(len(tokens) - k):
|
| 17 |
+
state = tuple(tokens[i : i + k])
|
| 18 |
+
chain[state].append(tokens[i + k])
|
| 19 |
+
|
| 20 |
+
# 3) Text-Generation function
|
| 21 |
+
def generate_text():
|
| 22 |
+
# pick a random starting state
|
| 23 |
+
state = random.choice(list(chain.keys()))
|
| 24 |
+
output = list(state)
|
| 25 |
+
|
| 26 |
+
# generate 200 words total (including the k seed words)
|
| 27 |
+
for _ in range(200 - k):
|
| 28 |
+
nxt_choices = chain.get(state)
|
| 29 |
+
if not nxt_choices:
|
| 30 |
+
# dead end β pick a fresh state
|
| 31 |
+
state = random.choice(list(chain.keys()))
|
| 32 |
+
output.extend(state)
|
| 33 |
+
continue
|
| 34 |
+
nxt = random.choice(nxt_choices)
|
| 35 |
+
output.append(nxt)
|
| 36 |
+
state = tuple(output[-k:])
|
| 37 |
+
return " ".join(output)
|
| 38 |
+
|
| 39 |
+
# 4) Gradio UI
|
| 40 |
+
with gr.Blocks() as demo:
|
| 41 |
+
gr.Markdown("## Planet Earth Markov Generator")
|
| 42 |
+
output_box = gr.Textbox(label="Generated Text", lines=10)
|
| 43 |
+
gr.Button("Generate").click(fn=generate_text, outputs=output_box)
|
| 44 |
+
|
| 45 |
+
if __name__ == "__main__":
|
|
|
|
| 46 |
demo.launch(share=True)
|