Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import tensorflow as tf
|
2 |
+
from tensorflow.keras.preprocessing.sequence import pad_sequences
|
3 |
+
from tensorflow.keras.models import load_model
|
4 |
+
from tensorflow.keras.preprocessing.text import Tokenizer
|
5 |
+
from tensorflow.keras import backend as K
|
6 |
+
import pandas as pd
|
7 |
+
import numpy as np
|
8 |
+
import re
|
9 |
+
import gradio as gr
|
10 |
+
|
11 |
+
# Function to clean text
|
12 |
+
def clean_text(text):
|
13 |
+
text = text.lower()
|
14 |
+
text = re.sub(r"[^a-zA-Zñḳḍāī\s]", "", text)
|
15 |
+
text = re.sub(r'(\n)(\S)', r'\1 \2', text)
|
16 |
+
return text
|
17 |
+
|
18 |
+
# Load the dataset
|
19 |
+
df = pd.read_csv('Roman-Urdu-Poetry.csv')
|
20 |
+
df['Poetry'] = df['Poetry'].apply(clean_text)
|
21 |
+
|
22 |
+
# Tokenization
|
23 |
+
tokenizer = Tokenizer(num_words=5000, filters='')
|
24 |
+
tokenizer.fit_on_texts(df['Poetry'])
|
25 |
+
sequences = tokenizer.texts_to_sequences(df['Poetry'])
|
26 |
+
|
27 |
+
max_sequence_len = max([len(seq) for seq in sequences])
|
28 |
+
max_sequence_len = min(max_sequence_len, 225)
|
29 |
+
padded_sequences = pad_sequences(sequences, maxlen=max_sequence_len, padding='pre')
|
30 |
+
K.clear_session()
|
31 |
+
|
32 |
+
input_sequences = []
|
33 |
+
output_words = []
|
34 |
+
|
35 |
+
for seq in padded_sequences:
|
36 |
+
for i in range(1, len(seq)):
|
37 |
+
input_sequences.append(seq[:i])
|
38 |
+
output_words.append(seq[i])
|
39 |
+
|
40 |
+
input_sequences = pad_sequences(input_sequences, maxlen=max_sequence_len, padding='pre')
|
41 |
+
output_words = np.array(output_words)
|
42 |
+
total_words = len(tokenizer.word_index) + 1
|
43 |
+
|
44 |
+
|
45 |
+
# Load the trained model
|
46 |
+
model = load_model('poetry_model.h5')
|
47 |
+
|
48 |
+
# Function to generate poetry
|
49 |
+
def generate_poem(seed_text, next_words, temperature):
|
50 |
+
for _ in range(next_words):
|
51 |
+
token_list = tokenizer.texts_to_sequences([seed_text])[0]
|
52 |
+
token_list = pad_sequences([token_list], maxlen=max_sequence_len-1, padding='pre')
|
53 |
+
|
54 |
+
predictions = model.predict(token_list, verbose=0)[0]
|
55 |
+
|
56 |
+
# Apply temperature scaling
|
57 |
+
predictions = np.log(predictions + 1e-10) / temperature
|
58 |
+
exp_preds = np.exp(predictions)
|
59 |
+
predictions = exp_preds / np.sum(exp_preds)
|
60 |
+
|
61 |
+
# Sample the next word
|
62 |
+
predicted_word_index = np.random.choice(len(predictions), p=predictions)
|
63 |
+
predicted_word = tokenizer.index_word.get(predicted_word_index, '')
|
64 |
+
|
65 |
+
if predicted_word:
|
66 |
+
seed_text += " " + predicted_word
|
67 |
+
|
68 |
+
return seed_text
|
69 |
+
|
70 |
+
# Custom CSS Styling
|
71 |
+
custom_css = """
|
72 |
+
body {
|
73 |
+
background-color: #121212;
|
74 |
+
color: white;
|
75 |
+
font-family: 'Arial', sans-serif;
|
76 |
+
}
|
77 |
+
.gradio-container {
|
78 |
+
max-width: 600px;
|
79 |
+
margin: auto;
|
80 |
+
text-align: center;
|
81 |
+
}
|
82 |
+
textarea, input, button {
|
83 |
+
font-size: 16px !important;
|
84 |
+
}
|
85 |
+
button {
|
86 |
+
background: #ff5c5c !important;
|
87 |
+
color: white !important;
|
88 |
+
padding: 12px 18px !important;
|
89 |
+
border-radius: 8px !important;
|
90 |
+
font-weight: bold;
|
91 |
+
border: none !important;
|
92 |
+
cursor: pointer;
|
93 |
+
}
|
94 |
+
button:hover {
|
95 |
+
background: #e74c3c !important;
|
96 |
+
}
|
97 |
+
"""
|
98 |
+
|
99 |
+
# Gradio Interface
|
100 |
+
with gr.Blocks(css=custom_css) as iface:
|
101 |
+
gr.Markdown("<h1 style='text-align: center;'>🎶 Verse Hub: Poetry Generator 🎶</h1>")
|
102 |
+
|
103 |
+
|
104 |
+
seed_text = gr.Textbox(label="Verse Hub", placeholder="Start your poetry...", interactive=True)
|
105 |
+
|
106 |
+
with gr.Row():
|
107 |
+
words = gr.Slider(minimum=5, maximum=100, step=1, value=10, label="Number of Words", interactive=True)
|
108 |
+
temperature = gr.Slider(minimum=0.5, maximum=2.0, step=0.1, value=1.0, label="Temperature", interactive=True)
|
109 |
+
|
110 |
+
generate_button = gr.Button("✨ Generate Poetry 🎤")
|
111 |
+
output_text = gr.Textbox(label="Generated Poem", interactive=False, lines=6)
|
112 |
+
|
113 |
+
generate_button.click(fn=generate_poem, inputs=[seed_text, words, temperature], outputs=output_text)
|
114 |
+
|
115 |
+
# Launch Gradio App
|
116 |
+
iface.launch()
|