heramb04 commited on
Commit
20074ea
Β·
verified Β·
1 Parent(s): 46641a7

Update app.py

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