Update app.py
Browse files
app.py
CHANGED
|
@@ -1,105 +1,37 @@
|
|
| 1 |
-
# -*- coding: utf-8 -*-
|
| 2 |
-
"""Flan-t5-xl_with_GPU.ipynb
|
| 3 |
-
|
| 4 |
-
Automatically generated by Colaboratory.
|
| 5 |
-
|
| 6 |
-
Original file is located at
|
| 7 |
-
https://colab.research.google.com/drive/15P4GWaUNFBqJf5_I58DxSiusjPiHUeU6
|
| 8 |
-
"""
|
| 9 |
-
|
| 10 |
-
#import gradio as gr
|
| 11 |
-
|
| 12 |
-
#def greet(name)
|
| 13 |
-
# return "Hello" + name + "!"
|
| 14 |
-
|
| 15 |
-
#iface = gr.Interface(fn=greet, inputs="text", outputs="text")
|
| 16 |
-
#iface.launch()
|
| 17 |
-
|
| 18 |
-
#theme = gr.themes.Soft().set(
|
| 19 |
-
# body_background_fill='*background_fill_secondary',
|
| 20 |
-
# body_text_color_subdued='*body_text_color',
|
| 21 |
-
# body_text_color_subdued_dark='*chatbot_code_background_color'
|
| 22 |
-
#)
|
| 23 |
-
|
| 24 |
-
#app = gr.Interface(
|
| 25 |
-
# fn=qa_result,
|
| 26 |
-
# btn=gr.UploadButton("📁", file_types=[".pdf", ".csv", ".doc"], ),
|
| 27 |
-
# inputs=['textbox', 'text', 'file'],
|
| 28 |
-
# outputs='textbox',
|
| 29 |
-
# title='Բարև՛, ինչպե՞ս ես։',
|
| 30 |
-
# theme=theme,
|
| 31 |
-
# description='Ի՞նչ հարցեր ունես։'
|
| 32 |
-
#)
|
| 33 |
-
|
| 34 |
-
import os
|
| 35 |
-
os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "max_split_size_mb:512"
|
| 36 |
-
|
| 37 |
-
from IPython.display import HTML, display
|
| 38 |
-
|
| 39 |
-
def set_css():
|
| 40 |
-
display(HTML('''
|
| 41 |
-
<style>
|
| 42 |
-
pre {
|
| 43 |
-
white-space: pre-wrap;
|
| 44 |
-
}
|
| 45 |
-
</style>
|
| 46 |
-
'''))
|
| 47 |
-
#get_ipython().events.register('pre_run_cell', set_css)
|
| 48 |
-
|
| 49 |
-
import multiprocessing
|
| 50 |
-
import torch
|
| 51 |
-
|
| 52 |
-
torch.cuda.empty_cache()
|
| 53 |
-
|
| 54 |
-
from deep_translator import GoogleTranslator
|
| 55 |
-
|
| 56 |
-
# Use any translator you like, in this example GoogleTranslator
|
| 57 |
-
#translated = GoogleTranslator(source='hy', target='en').translate("Բարև, ո՞նց ես։") # output -> Hello, how are you?
|
| 58 |
-
|
| 59 |
-
#device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
| 60 |
-
#device
|
| 61 |
-
|
| 62 |
import streamlit as st
|
| 63 |
-
|
| 64 |
-
#x = st.slider('Select a value')
|
| 65 |
-
#st.write(x, 'squared is', x * x)
|
| 66 |
-
|
| 67 |
import torch
|
| 68 |
from transformers import T5Tokenizer, T5ForConditionalGeneration
|
|
|
|
| 69 |
|
|
|
|
| 70 |
tokenizer = T5Tokenizer.from_pretrained("google/flan-t5-xl")
|
| 71 |
-
model = T5ForConditionalGeneration.from_pretrained("google/flan-t5-xl",
|
| 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 |
-
repetition_penalty = 2.1)
|
| 102 |
-
|
| 103 |
-
st.write(GoogleTranslator(source='en', target='hy').translate(tokenizer.decode(outputs[0], skip_special_tokens = True)))
|
| 104 |
-
#process()
|
| 105 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import torch
|
| 3 |
from transformers import T5Tokenizer, T5ForConditionalGeneration
|
| 4 |
+
from deep_translator import GoogleTranslator
|
| 5 |
|
| 6 |
+
# Initialize tokenizer and model
|
| 7 |
tokenizer = T5Tokenizer.from_pretrained("google/flan-t5-xl")
|
| 8 |
+
model = T5ForConditionalGeneration.from_pretrained("google/flan-t5-xl", device="cuda:0")
|
| 9 |
+
|
| 10 |
+
# Function to generate text
|
| 11 |
+
def generate_text(input_text):
|
| 12 |
+
input_ids = tokenizer(input_text, return_tensors="pt").input_ids.to("cuda:0")
|
| 13 |
+
|
| 14 |
+
outputs = model.generate(input_ids,
|
| 15 |
+
min_length=20,
|
| 16 |
+
max_new_tokens=600,
|
| 17 |
+
length_penalty=1.0,
|
| 18 |
+
num_beams=2,
|
| 19 |
+
no_repeat_ngram_size=3,
|
| 20 |
+
temperature=1.0,
|
| 21 |
+
top_k=150,
|
| 22 |
+
top_p=0.92,
|
| 23 |
+
repetition_penalty=2.1)
|
| 24 |
+
|
| 25 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 26 |
+
|
| 27 |
+
# Streamlit app
|
| 28 |
+
st.title("Text Generation with Flan-t5-xl")
|
| 29 |
+
translated_input = st.text_input("Enter your question in Armenian:")
|
| 30 |
+
if translated_input:
|
| 31 |
+
input_text = GoogleTranslator(source='hy', target='en').translate(translated_input)
|
| 32 |
+
generated_text = generate_text(input_text)
|
| 33 |
+
st.write("Generated Answer (Armenian):")
|
| 34 |
+
st.write(GoogleTranslator(source='en', target='hy').translate(generated_text))
|
| 35 |
+
torch.cuda.empty_cache() # Release GPU memory
|
| 36 |
+
|
| 37 |
+
# Note: The 'torch.cuda.empty_cache()' line releases GPU memory after generating each text.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|