Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import tensorflow as tf
|
| 3 |
+
import numpy as np
|
| 4 |
+
from tensorflow.keras.preprocessing.sequence import pad_sequences
|
| 5 |
+
import json
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
# Initialize global variables
|
| 9 |
+
model = None
|
| 10 |
+
tokenizer = None
|
| 11 |
+
max_len_seq = None
|
| 12 |
+
|
| 13 |
+
def load_model_artifacts():
|
| 14 |
+
global model, tokenizer, max_len_seq
|
| 15 |
+
|
| 16 |
+
# Load model directly from root directory
|
| 17 |
+
model = tf.keras.models.load_model('shakespeare_model.h5')
|
| 18 |
+
|
| 19 |
+
# Load tokenizer from root
|
| 20 |
+
with open('tokenizer.json', 'r') as f:
|
| 21 |
+
tokenizer = tf.keras.preprocessing.text.tokenizer_from_json(f.read())
|
| 22 |
+
|
| 23 |
+
# Load config from root
|
| 24 |
+
with open('config.json', 'r') as f:
|
| 25 |
+
config = json.load(f)
|
| 26 |
+
max_len_seq = config['max_len_seq']
|
| 27 |
+
|
| 28 |
+
def generate_shakespeare_quote(seed_text, num_words):
|
| 29 |
+
"""Generate Shakespeare-style text from a seed text"""
|
| 30 |
+
if model is None:
|
| 31 |
+
load_model_artifacts()
|
| 32 |
+
|
| 33 |
+
try:
|
| 34 |
+
for _ in range(int(num_words)):
|
| 35 |
+
# Convert the seed text to sequences
|
| 36 |
+
token_list = tokenizer.texts_to_sequences([seed_text])[0]
|
| 37 |
+
# Pad the sequences
|
| 38 |
+
token_list = pad_sequences([token_list], maxlen=max_len_seq-1, padding='pre')
|
| 39 |
+
# Predict the next word
|
| 40 |
+
predicted = model.predict(token_list, verbose=0)
|
| 41 |
+
# Get the word with highest probability
|
| 42 |
+
predicted_word = tokenizer.index_word[np.argmax(predicted, axis=-1).item()]
|
| 43 |
+
# Add the predicted word to the seed text
|
| 44 |
+
seed_text += " " + predicted_word
|
| 45 |
+
|
| 46 |
+
return seed_text
|
| 47 |
+
except Exception as e:
|
| 48 |
+
return f"Error generating text: {str(e)}"
|
| 49 |
+
|
| 50 |
+
# Create the Gradio interface
|
| 51 |
+
iface = gr.Interface(
|
| 52 |
+
fn=generate_shakespeare_quote,
|
| 53 |
+
inputs=[
|
| 54 |
+
gr.Textbox(
|
| 55 |
+
label="Enter your seed text",
|
| 56 |
+
placeholder="Start your quote here...",
|
| 57 |
+
value="to be or"
|
| 58 |
+
),
|
| 59 |
+
gr.Slider(
|
| 60 |
+
minimum=1,
|
| 61 |
+
maximum=50,
|
| 62 |
+
value=10,
|
| 63 |
+
step=1,
|
| 64 |
+
label="Number of words to generate"
|
| 65 |
+
)
|
| 66 |
+
],
|
| 67 |
+
outputs=gr.Textbox(label="Generated Quote"),
|
| 68 |
+
title="Shakespeare Quote Generator",
|
| 69 |
+
description="""Generate Shakespeare-style quotes using AI!
|
| 70 |
+
Enter a seed text and choose how many words you want to generate.
|
| 71 |
+
The model will continue your text in Shakespeare's style.""",
|
| 72 |
+
examples=[
|
| 73 |
+
["to be or", 10],
|
| 74 |
+
["love is", 15],
|
| 75 |
+
["life is", 12],
|
| 76 |
+
["death be not", 10],
|
| 77 |
+
["shall i compare thee", 8]
|
| 78 |
+
],
|
| 79 |
+
theme=gr.themes.Base()
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
+
# Launch the app
|
| 83 |
+
if __name__ == "__main__":
|
| 84 |
+
iface.launch()
|