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|
|
|
| 1 |
+
# app.py
|
| 2 |
+
|
| 3 |
+
import random
|
| 4 |
+
import csv
|
| 5 |
+
import gradio as gr
|
| 6 |
+
import pandas as pd
|
| 7 |
+
import tempfile
|
| 8 |
+
|
| 9 |
+
# Word lists
|
| 10 |
+
nouns = [
|
| 11 |
+
"dog", "cat", "child", "teacher", "artist", "bird", "river", "mountain",
|
| 12 |
+
"book", "city", "car", "tree", "flower", "student", "computer", "phone",
|
| 13 |
+
"house", "garden", "song", "idea", "scientist", "engineer", "doctor",
|
| 14 |
+
"chef", "musician", "athlete", "writer", "poet", "farmer", "pilot"
|
| 15 |
+
]
|
| 16 |
+
|
| 17 |
+
adjectives = [
|
| 18 |
+
"quick", "lazy", "beautiful", "tall", "short", "happy", "sad", "bright",
|
| 19 |
+
"dark", "colorful", "quiet", "loud", "new", "old", "young", "ancient",
|
| 20 |
+
"modern", "cold", "warm", "soft", "hard", "heavy", "light", "calm",
|
| 21 |
+
"stormy", "fresh", "strong", "weak", "brave"
|
| 22 |
+
]
|
| 23 |
+
|
| 24 |
+
verbs = [
|
| 25 |
+
"run", "jump", "paint", "read", "fly", "write", "sing", "build",
|
| 26 |
+
"create", "discover", "learn", "teach", "drive", "grow", "think",
|
| 27 |
+
"talk", "listen", "play", "see", "walk", "swim", "code", "design",
|
| 28 |
+
"cook", "dance", "draw", "study", "explore", "invent", "research"
|
| 29 |
+
]
|
| 30 |
+
|
| 31 |
+
adverbs = [
|
| 32 |
+
"quickly", "slowly", "gracefully", "happily", "sadly", "quietly", "loudly",
|
| 33 |
+
"brightly", "softly", "carefully", "eagerly", "angrily", "easily", "hardly",
|
| 34 |
+
"rarely", "often", "never", "always", "sometimes", "soon", "daily", "patiently",
|
| 35 |
+
"politely", "proudly", "silently", "warmly", "well", "badly", "closely", "deeply"
|
| 36 |
+
]
|
| 37 |
+
|
| 38 |
+
prepositions = [
|
| 39 |
+
"in", "on", "over", "under", "beside", "with", "without", "near",
|
| 40 |
+
"between", "through", "against", "among", "around", "before", "after",
|
| 41 |
+
"inside", "outside", "above", "below", "across", "behind", "beyond",
|
| 42 |
+
"during", "for", "from", "into", "like", "off", "toward"
|
| 43 |
+
]
|
| 44 |
+
|
| 45 |
+
articles = ["the", "a", "an"]
|
| 46 |
+
|
| 47 |
+
conjunctions = ["and", "but", "so", "because", "when", "while", "although", "if", "unless", "since"]
|
| 48 |
+
|
| 49 |
+
# Semantic rules: mapping nouns to appropriate verbs
|
| 50 |
+
noun_verb_map = {
|
| 51 |
+
"dog": ["run", "jump", "bark", "play", "walk"],
|
| 52 |
+
"cat": ["meow", "sleep", "jump", "play", "purr"],
|
| 53 |
+
"child": ["play", "learn", "read", "laugh", "grow"],
|
| 54 |
+
"teacher": ["teach", "explain", "guide", "help", "learn"],
|
| 55 |
+
"artist": ["paint", "draw", "create", "design", "imagine"],
|
| 56 |
+
"bird": ["fly", "sing", "chirp", "nest", "soar"],
|
| 57 |
+
"river": ["flow", "run", "wind", "bend", "swell"],
|
| 58 |
+
"mountain": ["stand", "tower", "rise", "loom", "shadow"],
|
| 59 |
+
"book": ["tell", "describe", "illustrate", "explain", "reveal"],
|
| 60 |
+
"city": ["grow", "expand", "develop", "bustle", "sleep"],
|
| 61 |
+
"car": ["drive", "speed", "stop", "park", "honk"],
|
| 62 |
+
"tree": ["grow", "sway", "stand", "shed", "bloom"],
|
| 63 |
+
"flower": ["bloom", "grow", "wilt", "open", "close"],
|
| 64 |
+
"student": ["study", "learn", "read", "write", "graduate"],
|
| 65 |
+
"computer": ["compute", "process", "run", "crash", "boot"],
|
| 66 |
+
"phone": ["ring", "vibrate", "charge", "die", "connect"],
|
| 67 |
+
"house": ["stand", "shelter", "protect", "age", "burn"],
|
| 68 |
+
"garden": ["grow", "bloom", "flourish", "wilt", "produce"],
|
| 69 |
+
"song": ["play", "sound", "echo", "resonate", "end"],
|
| 70 |
+
"idea": ["form", "grow", "develop", "emerge", "inspire"],
|
| 71 |
+
"scientist": ["research", "discover", "experiment", "study", "invent"],
|
| 72 |
+
"engineer": ["design", "build", "develop", "test", "solve"],
|
| 73 |
+
"doctor": ["heal", "diagnose", "treat", "operate", "care"],
|
| 74 |
+
"chef": ["cook", "prepare", "taste", "create", "serve"],
|
| 75 |
+
"musician": ["play", "compose", "perform", "sing", "record"],
|
| 76 |
+
"athlete": ["run", "train", "compete", "win", "lose"],
|
| 77 |
+
"writer": ["write", "create", "imagine", "edit", "publish"],
|
| 78 |
+
"poet": ["write", "compose", "imagine", "express", "rhyme"],
|
| 79 |
+
"farmer": ["grow", "plant", "harvest", "plow", "raise"],
|
| 80 |
+
"pilot": ["fly", "navigate", "land", "take off", "command"]
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
# Sentence templates
|
| 84 |
+
templates = [
|
| 85 |
+
"{Article} {adjective} {noun} {adverb} {verb}s {preposition} {article} {adjective} {noun2}.",
|
| 86 |
+
"{Article} {noun} {verb}s {preposition} {article} {noun2} {conjunction} {verb2}s {adverb}.",
|
| 87 |
+
"{Noun_plural} {adverb} {verb} {preposition} {noun2}.",
|
| 88 |
+
"{Noun} {verb}s {preposition} {article} {noun2} {conjunction} {article} {noun} {verb2}s.",
|
| 89 |
+
"{Article} {adjective} {noun} {verb}s {preposition} {noun2} {conjunction} {adverb} {verb2}s.",
|
| 90 |
+
"{Noun} {verb}s {article} {noun2} {preposition} {noun}."
|
| 91 |
+
]
|
| 92 |
+
|
| 93 |
+
def generate_sentence():
|
| 94 |
+
template = random.choice(templates)
|
| 95 |
+
noun = random.choice(nouns)
|
| 96 |
+
# Get appropriate verbs for noun
|
| 97 |
+
verbs_for_noun = noun_verb_map.get(noun, verbs)
|
| 98 |
+
verb = random.choice(verbs_for_noun)
|
| 99 |
+
|
| 100 |
+
noun2 = random.choice(nouns)
|
| 101 |
+
# Ensure noun2 is different from noun
|
| 102 |
+
while noun2 == noun:
|
| 103 |
+
noun2 = random.choice(nouns)
|
| 104 |
+
# Get appropriate verbs for noun2
|
| 105 |
+
verbs_for_noun2 = noun_verb_map.get(noun2, verbs)
|
| 106 |
+
verb2 = random.choice(verbs_for_noun2)
|
| 107 |
+
|
| 108 |
+
sentence = template.format(
|
| 109 |
+
Article=random.choice(articles).capitalize(),
|
| 110 |
+
article=random.choice(articles),
|
| 111 |
+
adjective=random.choice(adjectives),
|
| 112 |
+
noun=noun,
|
| 113 |
+
noun2=noun2,
|
| 114 |
+
Noun=noun.capitalize(),
|
| 115 |
+
Noun_plural=noun.capitalize() + "s",
|
| 116 |
+
verb=verb,
|
| 117 |
+
verb2=verb2,
|
| 118 |
+
adverb=random.choice(adverbs),
|
| 119 |
+
preposition=random.choice(prepositions),
|
| 120 |
+
conjunction=random.choice(conjunctions)
|
| 121 |
+
)
|
| 122 |
+
# Capitalize the first letter and ensure proper punctuation
|
| 123 |
+
sentence = sentence.capitalize()
|
| 124 |
+
if not sentence.endswith('.'):
|
| 125 |
+
sentence += '.'
|
| 126 |
+
return sentence
|
| 127 |
+
|
| 128 |
+
def generate_sentences(num_sentences):
|
| 129 |
+
sentences = [generate_sentence() for _ in range(int(num_sentences))]
|
| 130 |
+
df = pd.DataFrame(sentences, columns=["sentence"])
|
| 131 |
+
# Save to a temporary CSV file
|
| 132 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".csv")
|
| 133 |
+
df.to_csv(temp_file.name, index=False)
|
| 134 |
+
return temp_file.name
|
| 135 |
+
|
| 136 |
+
def generate_and_download(num_sentences):
|
| 137 |
+
csv_file = generate_sentences(num_sentences)
|
| 138 |
+
return csv_file
|
| 139 |
+
|
| 140 |
+
# Gradio Interface
|
| 141 |
+
with gr.Blocks() as demo:
|
| 142 |
+
gr.Markdown(
|
| 143 |
+
"""
|
| 144 |
+
# Sentence Dataset Generator with Semantic Rules
|
| 145 |
+
|
| 146 |
+
Enter the number of sentences you want to generate, and download a CSV file containing the sentences.
|
| 147 |
+
|
| 148 |
+
This generator uses semantic rules to create more coherent and meaningful sentences.
|
| 149 |
+
"""
|
| 150 |
+
)
|
| 151 |
+
num_sentences = gr.Number(label="Number of Sentences", value=1000, precision=0)
|
| 152 |
+
output = gr.File(label="Download CSV")
|
| 153 |
+
generate_button = gr.Button("Generate Sentences")
|
| 154 |
+
generate_button.click(
|
| 155 |
+
fn=generate_and_download,
|
| 156 |
+
inputs=num_sentences,
|
| 157 |
+
outputs=output
|
| 158 |
+
)
|
| 159 |
+
|
| 160 |
+
if __name__ == "__main__":
|
| 161 |
+
demo.launch()
|