Spaces:
Sleeping
Sleeping
Commit
·
d073dcb
1
Parent(s):
d274ee0
Update gradio_app.py
Browse files- gradio_app.py +3 -39
gradio_app.py
CHANGED
@@ -1,20 +1,9 @@
|
|
1 |
# Importing libraries
|
2 |
-
from transformers import M2M100Tokenizer, M2M100ForConditionalGeneration
|
3 |
from llama_cpp import Llama
|
4 |
import gradio as gr
|
5 |
import psutil
|
6 |
|
7 |
# Initing things
|
8 |
-
print("! DOWNLOADING TOKENIZER AND SETTING ALL UP !")
|
9 |
-
translator_tokenizer = M2M100Tokenizer.from_pretrained( # tokenizer for translator
|
10 |
-
"facebook/m2m100_418M", cache_dir="translator/"
|
11 |
-
)
|
12 |
-
print("! DOWNLOADING MODEL AND SETTING ALL UP !")
|
13 |
-
translator_model = M2M100ForConditionalGeneration.from_pretrained( # translator model
|
14 |
-
"facebook/m2m100_418M", cache_dir="translator/"
|
15 |
-
)
|
16 |
-
print("! SETTING MODEL IN EVALUATION MODE !")
|
17 |
-
translator_model.eval()
|
18 |
print("! INITING LLAMA MODEL !")
|
19 |
llm = Llama(model_path="./model.bin") # LLaMa model
|
20 |
llama_model_name = "TheBloke/dolphin-2.2.1-AshhLimaRP-Mistral-7B-GGUF"
|
@@ -35,21 +24,6 @@ Or you can once follow steps in Dockerfile and try it on your machine, not in Do
|
|
35 |
<br>''' + f"Memory used: {psutil.virtual_memory()[2]}<br>" + '''
|
36 |
Powered by <a href="https://github.com/abetlen/llama-cpp-python">llama-cpp-python</a> and <a href="https://www.gradio.app/">Gradio</a>.<br><br>'''
|
37 |
|
38 |
-
'''
|
39 |
-
# Defining languages for translator (i just chose popular on my opinion languages!!!)
|
40 |
-
ru - Russian
|
41 |
-
uk - Ukranian
|
42 |
-
zh - Chinese
|
43 |
-
de - German
|
44 |
-
fr - French
|
45 |
-
hi - Hindi
|
46 |
-
it - Italian
|
47 |
-
ja - Japanese
|
48 |
-
es - Spanish
|
49 |
-
ar - Arabic
|
50 |
-
'''
|
51 |
-
languages = ["ru", "uk", "zh", "de", "fr", "hi", "it", "ja", "es", "ar"]
|
52 |
-
|
53 |
# Loading prompt
|
54 |
with open('system.prompt', 'r', encoding='utf-8') as f:
|
55 |
prompt = f.read()
|
@@ -77,21 +51,11 @@ def generate_answer(request: str, max_tokens: int = 256, language: str = "en", c
|
|
77 |
break
|
78 |
counter += 1
|
79 |
logs += f"Final attempt: {counter}\n"
|
80 |
-
if len(text.strip())
|
|
|
81 |
text = "Sorry, but something went wrong while generating answer. Try again or fix code. If you are maintainer of this space, look into logs."
|
82 |
|
83 |
-
|
84 |
-
logs += f"\nTranslating from en to {language}"
|
85 |
-
encoded_input = translator_tokenizer(text, return_tensors="pt")
|
86 |
-
generated_tokens = translator_model.generate(
|
87 |
-
**encoded_input, forced_bos_token_id=translator_tokenizer.get_lang_id(language)
|
88 |
-
)
|
89 |
-
translated_text = translator_tokenizer.batch_decode(
|
90 |
-
generated_tokens, skip_special_tokens=True
|
91 |
-
)[0]
|
92 |
-
logs += f"\nTranslated: {translated_text}\nOriginal: {text}"
|
93 |
-
return translated_text, logs
|
94 |
-
logs += f"\nOriginal: {text}"
|
95 |
return text, logs
|
96 |
except Exception as e:
|
97 |
print(e)
|
|
|
1 |
# Importing libraries
|
|
|
2 |
from llama_cpp import Llama
|
3 |
import gradio as gr
|
4 |
import psutil
|
5 |
|
6 |
# Initing things
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
print("! INITING LLAMA MODEL !")
|
8 |
llm = Llama(model_path="./model.bin") # LLaMa model
|
9 |
llama_model_name = "TheBloke/dolphin-2.2.1-AshhLimaRP-Mistral-7B-GGUF"
|
|
|
24 |
<br>''' + f"Memory used: {psutil.virtual_memory()[2]}<br>" + '''
|
25 |
Powered by <a href="https://github.com/abetlen/llama-cpp-python">llama-cpp-python</a> and <a href="https://www.gradio.app/">Gradio</a>.<br><br>'''
|
26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
# Loading prompt
|
28 |
with open('system.prompt', 'r', encoding='utf-8') as f:
|
29 |
prompt = f.read()
|
|
|
51 |
break
|
52 |
counter += 1
|
53 |
logs += f"Final attempt: {counter}\n"
|
54 |
+
if len(text.strip()) <= 1 or text.strip() in ['', None, ' ']:
|
55 |
+
logs += f"Generated and aborted: {text}"
|
56 |
text = "Sorry, but something went wrong while generating answer. Try again or fix code. If you are maintainer of this space, look into logs."
|
57 |
|
58 |
+
logs += f"\nFinal: '''{text}'''"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
return text, logs
|
60 |
except Exception as e:
|
61 |
print(e)
|