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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +97 -32
app.py CHANGED
@@ -1,37 +1,102 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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.
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """Flan-t5-xl_with_GPU.ipynb
3
+ Automatically generated by Colaboratory.
4
+ Original file is located at
5
+ https://colab.research.google.com/drive/15P4GWaUNFBqJf5_I58DxSiusjPiHUeU6
6
+ """
7
+
8
+ #import gradio as gr
9
+
10
+ #def greet(name)
11
+ # return "Hello" + name + "!"
12
+
13
+ #iface = gr.Interface(fn=greet, inputs="text", outputs="text")
14
+ #iface.launch()
15
+
16
+ #theme = gr.themes.Soft().set(
17
+ # body_background_fill='*background_fill_secondary',
18
+ # body_text_color_subdued='*body_text_color',
19
+ # body_text_color_subdued_dark='*chatbot_code_background_color'
20
+ #)
21
+
22
+ #app = gr.Interface(
23
+ # fn=qa_result,
24
+ # btn=gr.UploadButton("📁", file_types=[".pdf", ".csv", ".doc"], ),
25
+ # inputs=['textbox', 'text', 'file'],
26
+ # outputs='textbox',
27
+ # title='Բարև՛, ինչպե՞ս ես։',
28
+ # theme=theme,
29
+ # description='Ի՞նչ հարցեր ունես։'
30
+ #)
31
+
32
+ import os
33
+ os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "max_split_size_mb:512"
34
+
35
+ from IPython.display import HTML, display
36
+
37
+ def set_css():
38
+ display(HTML('''
39
+ <style>
40
+ pre {
41
+ white-space: pre-wrap;
42
+ }
43
+ </style>
44
+ '''))
45
+ #get_ipython().events.register('pre_run_cell', set_css)
46
+
47
+ import multiprocessing
48
+ import torch
49
+
50
+ torch.cuda.empty_cache()
51
+
52
+ from deep_translator import GoogleTranslator
53
+
54
+ # Use any translator you like, in this example GoogleTranslator
55
+ #translated = GoogleTranslator(source='hy', target='en').translate("Բարև, ո՞նց ես։") # output -> Hello, how are you?
56
+
57
+ #device = "cuda:0" if torch.cuda.is_available() else "cpu"
58
+ #device
59
+
60
  import streamlit as st
61
+
62
+ #x = st.slider('Select a value')
63
+ #st.write(x, 'squared is', x * x)
64
+
65
  import torch
66
  from transformers import T5Tokenizer, T5ForConditionalGeneration
 
67
 
 
68
  tokenizer = T5Tokenizer.from_pretrained("google/flan-t5-xl")
69
+ model = T5ForConditionalGeneration.from_pretrained("google/flan-t5-xl", device_map = "auto")
70
+
71
+ # We are running FP32!
72
+
73
+ #my_text = "Summarize: \
74
+ #Science can ignite new discoveries for society, \
75
+ #Society has the tendency to refer to old, familiar ways of doing. \
76
+ #Chaos is also a part of our society, although increasingly often so.\
77
+ #Innovative ways lead to new growthin businesses and under certain conditions all of society participates."
78
+
79
+ #my_text = "Write an essay with 100 words about Quantum Physics and it's problems regarding our understanding of laws of Physics."
80
+
81
+ #my_text = "Q: Can Geoffrey Hinton have a conversation with George Washington? Give the rationale before answering."
82
+ # my_text = "A short explanation of machine learning for medical applications."
83
+
84
+ #def process():
85
+ translated = st.text_input("Գրեք ձեր հարցը: ")
86
+ my_text = GoogleTranslator(source='hy', target='en').translate(translated)
87
+
88
+ input_ids = tokenizer(my_text, return_tensors = "pt").input_ids.to("cuda")
89
+
90
+ outputs = model.generate(input_ids,
91
+ min_length = 20,
92
+ max_new_tokens = 600,
93
+ length_penalty = 1.0, # Set to values < 1.0 in order to encourage the model to generate shorter answers.
94
+ num_beams = 2,
95
+ no_repeat_ngram_size = 3,
96
+ temperature = 1.0,
97
+ top_k = 150, # default 50
98
+ top_p = 0.92,
99
+ repetition_penalty = 2.1)
100
+
101
+ st.write(GoogleTranslator(source='en', target='hy').translate(tokenizer.decode(outputs[0], skip_special_tokens = True)))
102
+ #process()