Obai33 commited on
Commit
b5924f6
·
verified ·
1 Parent(s): ab5b302

initial commit

Browse files
Files changed (1) hide show
  1. app.py +127 -0
app.py ADDED
@@ -0,0 +1,127 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """Copy of russian model testing.ipynb
3
+
4
+ Automatically generated by Colab.
5
+
6
+ Original file is located at
7
+ https://colab.research.google.com/drive/1c9k49wiWEvDa1zxIw65pUAsuzMlFn-tq
8
+ """
9
+
10
+ !pip install gradio
11
+ !pip install translate
12
+
13
+ import nltk
14
+ from nltk.tokenize import word_tokenize
15
+ from nltk.corpus import stopwords
16
+
17
+ import pandas as pd
18
+
19
+ import numpy as np
20
+ import tensorflow as tf
21
+ from tensorflow.keras.preprocessing.text import Tokenizer
22
+ from tensorflow.keras.preprocessing.sequence import pad_sequences
23
+ from tensorflow.keras.models import Sequential
24
+ from tensorflow.keras.layers import Embedding, LSTM, Dense, Bidirectional
25
+ from nltk.corpus import stopwords
26
+ from nltk.stem import WordNetLemmatizer
27
+ import nltk
28
+ from nltk.translate.bleu_score import sentence_bleu
29
+
30
+ nltk.download('stopwords')
31
+ nltk.download('wordnet')
32
+ nltk.download('punkt')
33
+
34
+ url = 'https://raw.githubusercontent.com/Obai33/NLP_PoemGenerationDatasets/main/russianpoems.csv'
35
+ text_data = pd.read_csv(url)
36
+
37
+ # removing duplicates and missing values
38
+ text_data.drop_duplicates(inplace = True)
39
+ text_data.dropna(inplace = True)
40
+ text_data
41
+
42
+ text_data = text_data['text']
43
+ text_data = text_data[500:700]
44
+
45
+ # Tokenization and lowercasing
46
+ tokenizer = Tokenizer()
47
+ tokenizer.fit_on_texts(text_data)
48
+
49
+ total_words = len(tokenizer.word_index) + 1
50
+
51
+ input_sequences = []
52
+ for line in text_data:
53
+ token_list = tokenizer.texts_to_sequences([line])[0]
54
+ for i in range(1, len(token_list)):
55
+ n_gram_sequence = token_list[:i+1]
56
+ input_sequences.append(n_gram_sequence)
57
+
58
+
59
+ # pad sequences
60
+ max_sequence_len = 100
61
+ input_sequences = np.array(pad_sequences(input_sequences, maxlen=max_sequence_len, padding='pre'))
62
+
63
+ # create predictors and label
64
+ xs, labels = input_sequences[:,:-1],input_sequences[:,-1]
65
+
66
+ ys = tf.keras.utils.to_categorical(labels, num_classes=total_words)
67
+
68
+ import requests
69
+ # URL of the model
70
+ url = 'https://github.com/Obai33/NLP_PoemGenerationDatasets/raw/main/modelrus1.h5'
71
+ # Local file path to save the model
72
+ local_filename = 'modelrus1.h5'
73
+
74
+ # Download the model file
75
+ response = requests.get(url)
76
+ with open(local_filename, 'wb') as f:
77
+ f.write(response.content)
78
+
79
+ # Load the pre-trained model
80
+ model = tf.keras.models.load_model(local_filename)
81
+
82
+ # Import the necessary library for translation
83
+ import translate
84
+
85
+ # Function to translate text to English
86
+ def translate_to_english(text):
87
+ translator = translate.Translator(from_lang="ru", to_lang="en")
88
+ translated_text = translator.translate(text)
89
+ return translated_text
90
+
91
+ def generate_russian_text(seed_text, next_words=50):
92
+ generated_text = seed_text
93
+ for _ in range(next_words):
94
+ token_list = tokenizer.texts_to_sequences([generated_text])[0]
95
+ token_list = pad_sequences([token_list], maxlen=max_sequence_len-1, padding='pre')
96
+ predicted = np.argmax(model.predict(token_list), axis=-1)
97
+ output_word = ""
98
+ for word, index in tokenizer.word_index.items():
99
+ if index == predicted:
100
+ output_word = word
101
+ break
102
+ generated_text += " " + output_word
103
+
104
+ '''
105
+ token_list = tokenizer.encode(generated_text, add_special_tokens=False)
106
+ token_list = pad_sequences([token_list], maxlen=max_sequence_len-1, padding='pre')
107
+ predicted = np.argmax(model.predict(token_list), axis=-1)
108
+ output_word = tokenizer.decode(predicted[0])
109
+ generated_text += " " + output_word
110
+ '''
111
+ #reconnected_text = generated_text.replace(" ##", "")
112
+ t_text = translate_to_english(generated_text)
113
+ return generated_text, t_text
114
+
115
+ import gradio as gr
116
+
117
+ # Update Gradio interface to include both Arabic and English outputs
118
+ iface = gr.Interface(
119
+ fn=generate_russian_text,
120
+ inputs="text",
121
+ outputs=["text", "text"],
122
+ title="Russian Poetry Generation",
123
+ description="Enter Russian text to generate a small poem.",
124
+ theme="compact"
125
+ )
126
+ # Run the interface
127
+ iface.launch()