Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,33 +1,34 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
-
from
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
self._prepare_inputs_for_inference()
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
# Prepare inputs for inference with appropriate padding and eos_token_id
|
| 13 |
-
self.padding = "max_length"
|
| 14 |
-
self.use_cache = True
|
| 15 |
-
self.eos_token_id = self.config.eos_token_id if self.config.eos_token_id else 50256
|
| 16 |
-
return None
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
tokenizer = GPT2Tokenizer.from_pretrained("gpt2", use_auth_token="your_huggingface_api_key")
|
| 21 |
-
model = CustomGPT2LMHeadModel.from_pretrained("gpt2", use_auth_token="your_huggingface_api_key")
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
# Decode and print the generated text
|
| 30 |
-
generated_text = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 31 |
-
print(generated_text)
|
| 32 |
-
|
| 33 |
-
print(generate_text(' '.join(input_words)))
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import tensorflow as tf
|
| 3 |
+
from tensorflow.keras.preprocessing.sequence import pad_sequences
|
| 4 |
+
import numpy as np
|
| 5 |
+
import pickle
|
| 6 |
|
| 7 |
+
# ํ ํฌ๋์ด์ ๋ถ๋ฌ์ค๊ธฐ
|
| 8 |
+
with open('tokenizer.pickle', 'rb') as handle:
|
| 9 |
+
tokenizer = pickle.load(handle)
|
|
|
|
| 10 |
|
| 11 |
+
# ํ
์คํธ ์์ฑ ๋ชจ๋ธ ๋ถ๋ฌ์ค๊ธฐ
|
| 12 |
+
model = tf.keras.models.load_model("text_generation_model")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
+
# ์ฌ์ฉ์ ์
๋ ฅ ๋ฐ๊ธฐ
|
| 15 |
+
user_input = input("ํ
์คํธ ์์ฑ์ ์์ํ ๋จ์ด๋ฅผ ์
๋ ฅํ์ธ์: ")
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
# ํ
์คํธ ์์ฑ ํจ์ ์ ์
|
| 18 |
+
def generate_text(seed_text, next_words, max_sequence_len):
|
| 19 |
+
for _ in range(next_words):
|
| 20 |
+
token_list = tokenizer.texts_to_sequences([seed_text])[0]
|
| 21 |
+
token_list = pad_sequences([token_list], maxlen=max_sequence_len-1, padding='pre')
|
| 22 |
+
predicted = model.predict_classes(token_list, verbose=0)
|
| 23 |
+
|
| 24 |
+
output_word = ""
|
| 25 |
+
for word, index in tokenizer.word_index.items():
|
| 26 |
+
if index == predicted:
|
| 27 |
+
output_word = word
|
| 28 |
+
break
|
| 29 |
+
seed_text += " " + output_word
|
| 30 |
+
return seed_text
|
| 31 |
|
| 32 |
+
# ํ
์คํธ ์์ฑ
|
| 33 |
+
generated_text = generate_text(user_input, next_words=50, max_sequence_len=25)
|
| 34 |
+
print(generated_text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|