-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
124 lines (104 loc) · 5.98 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
from flask import Flask, render_template, request
from transformers import pipeline
import logging
logging.basicConfig(level=logging.DEBUG)
app = Flask(__name__, static_folder='static')
def generate_gpt3_text(text):
generator = pipeline(task='text-generation', model='EleutherAI/gpt-neo-2.7B')
generated_text = generator(text, max_length=200, num_return_sequences=1, truncation=True)
return generated_text[0]['generated_text']
def generate_gpt2_text(prompt, max_length):
generator = pipeline('text-generation', model='gpt2')
generated_text = generator(prompt, max_length=max_length, num_return_sequences=1, truncation=True)
return generated_text[0]['generated_text']
def translate_text_t5(prompt):
translator = pipeline('translation_en_to_fr', model='t5-small')
translated_text = translator(prompt, max_length=100)[0]['translation_text']
return translated_text
def translate_text_english_to_hindi(prompt):
translator = pipeline('translation_en_to_hi', model='Helsinki-NLP/opus-mt-en-hi')
translated_text = translator(prompt, max_length=100)[0]['translation_text']
logging.debug(f'Generated text from GPT-3: {translated_text}')
print('Translated Text (English to French):', translated_text)
return translated_text
def translate_text_hindi_to_english(prompt):
translator = pipeline('translation_hi_to_en', model='Helsinki-NLP/opus-mt-hi-en')
translated_text = translator(prompt, max_length=100)[0]['translation_text']
return translated_text
def translate_text_spanish_to_english(prompt):
translator = pipeline('translation_es_to_en', model='Helsinki-NLP/opus-mt-es-en')
translated_text = translator(prompt, max_length=100)[0]['translation_text']
return translated_text
def translate_text_german_to_english(prompt):
translator = pipeline('translation_de_to_en', model='Helsinki-NLP/opus-mt-de-en')
translated_text = translator(prompt, max_length=100)[0]['translation_text']
return translated_text
def translate_text_french_to_english(prompt):
translator = pipeline('translation_fr_to_en', model='Helsinki-NLP/opus-mt-fr-en')
translated_text = translator(prompt, max_length=100)[0]['translation_text']
return translated_text
def translate_text_chinese_to_english(prompt):
translator = pipeline('translation_zh_to_en', model='Helsinki-NLP/opus-mt-zh-en')
translated_text = translator(prompt, max_length=100)[0]['translation_text']
return translated_text
def generate_long_content(input_text):
summarizer = pipeline('summarization', model='t5-small')
input_format = "summarize: {}".format(input_text)
generated_summary = summarizer(input_format, max_length=210, num_return_sequences=1, truncation=True)
output_summary = generated_summary[0]['summary_text']
return output_summary
def generate_text_bert(prompt):
generator = pipeline('fill-mask', model='bert-base-uncased')
generated_text = generator(prompt)
generated_sequences = [result['sequence'] for result in generated_text]
return generated_sequences
@app.route('/', methods=['GET', 'POST'])
def home():
generated_text = ''
if request.method == 'POST':
try:
prompt = request.form['prompt']
model_type = request.form['model_type']
logging.debug(f'Prompt received: {prompt}')
logging.debug(f'Model type selected: {model_type}')
if model_type == 'gpt3':
generated_text = generate_gpt3_text(prompt)
logging.debug(f'Generated text from GPT-3: {generated_text}')
elif model_type == 'gpt2':
max_length = int(request.form['max_length'])
generated_text = generate_gpt2_text(prompt, max_length)
logging.debug(f'Generated text from GPT-2: {generated_text}')
elif model_type == 'translation_en_to_fr':
max_length = int(request.form['max_length'])
generated_text = translate_text_t5(prompt)
logging.debug(f'Generated text from GPT-2: {generated_text}')
elif model_type == 'translation_en_to_hi':
generated_text = translate_text_english_to_hindi(prompt)
logging.debug(f'Generated text from GPT-2: {generated_text}')
elif model_type == 'translation_hi_to_en':
generated_text = translate_text_hindi_to_english(prompt)
logging.debug(f'Generated text from GPT-2: {generated_text}')
elif model_type == 'translation_es_to_en':
generated_text = translate_text_spanish_to_english(prompt)
logging.debug(f'Generated text from GPT-2: {generated_text}')
elif model_type == 'translation_de_to_en':
generated_text = translate_text_german_to_english(prompt)
logging.debug(f'Generated text from GPT-2: {generated_text}')
elif model_type == 'translation_fr_to_en':
generated_text = translate_text_french_to_english(prompt)
logging.debug(f'Generated text from GPT-2: {generated_text}')
elif model_type == 'translation_zh_to_en':
generated_text = translate_text_chinese_to_english(prompt)
logging.debug(f'Generated text from GPT-2: {generated_text}')
elif model_type == 'summarization':
generated_text = generate_long_content(prompt)
logging.debug(f'Generated text from T5: {generated_text}')
elif model_type == 'Text_bert':
generated_text = generate_text_bert(prompt)
logging.debug(f'Generated text from BERT: {generated_text}')
except Exception as e:
logging.error(f'An error occurred: {str(e)}')
return render_template('index.html', prompt=prompt, generated_text=generated_text)
return render_template('index.html', generated_text=generated_text)
if __name__ == '__main__':
app.run(debug=True)