Zaven commited on
Commit
e5304c4
·
1 Parent(s): b859279

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -100
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", device_map = "auto")
72
-
73
- # We are running FP32!
74
-
75
- #my_text = "Summarize: \
76
- #Science can ignite new discoveries for society, \
77
- #Society has the tendency to refer to old, familiar ways of doing. \
78
- #Chaos is also a part of our society, although increasingly often so.\
79
- #Innovative ways lead to new growthin businesses and under certain conditions all of society participates."
80
-
81
- #my_text = "Write an essay with 100 words about Quantum Physics and it's problems regarding our understanding of laws of Physics."
82
-
83
- #my_text = "Q: Can Geoffrey Hinton have a conversation with George Washington? Give the rationale before answering."
84
- # my_text = "A short explanation of machine learning for medical applications."
85
-
86
- #def process():
87
- translated = st.text_input("Գրեք ձեր հարցը: ")
88
- my_text = GoogleTranslator(source='hy', target='en').translate(translated)
89
-
90
- input_ids = tokenizer(my_text, return_tensors = "pt").input_ids.to("cuda")
91
-
92
- outputs = model.generate(input_ids,
93
- min_length = 20,
94
- max_new_tokens = 600,
95
- length_penalty = 1.0, # Set to values < 1.0 in order to encourage the model to generate shorter answers.
96
- num_beams = 2,
97
- no_repeat_ngram_size = 3,
98
- temperature = 1.0,
99
- top_k = 150, # default 50
100
- top_p = 0.92,
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.