Spaces:
Running
on
Zero
Running
on
Zero
aixsatoshi
commited on
Commit
•
d851055
1
Parent(s):
c0dc977
Update app.py
Browse files
app.py
CHANGED
@@ -1,63 +1,81 @@
|
|
1 |
import gradio as gr
|
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 |
demo = gr.ChatInterface(
|
46 |
-
|
|
|
|
|
47 |
additional_inputs=[
|
48 |
-
gr.
|
49 |
-
gr.Slider(
|
50 |
-
gr.
|
51 |
-
|
52 |
-
minimum=0.1,
|
53 |
-
maximum=1.0,
|
54 |
-
value=0.95,
|
55 |
-
step=0.05,
|
56 |
-
label="Top-p (nucleus sampling)",
|
57 |
-
),
|
58 |
-
],
|
59 |
)
|
60 |
|
61 |
-
|
62 |
-
if __name__ == "__main__":
|
63 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, StoppingCriteria, StoppingCriteriaList, TextIteratorStreamer
|
4 |
+
from threading import Thread
|
5 |
+
import spaces
|
6 |
+
|
7 |
+
# 言語リスト
|
8 |
+
languages = [
|
9 |
+
"English", "Chinese (Simplified)", "Chinese (Traditional)", "Spanish", "Arabic", "Hindi",
|
10 |
+
"Bengali", "Portuguese", "Russian", "Japanese", "German", "French", "Urdu", "Indonesian",
|
11 |
+
"Italian", "Turkish", "Korean", "Vietnamese", "Tamil", "Marathi", "Telugu", "Persian",
|
12 |
+
"Polish", "Dutch", "Thai", "Gujarati", "Romanian", "Ukrainian", "Malay", "Kannada", "Oriya (Odia)",
|
13 |
+
"Burmese (Myanmar)", "Azerbaijani", "Uzbek", "Kurdish (Kurmanji)", "Swedish", "Filipino (Tagalog)",
|
14 |
+
"Serbian", "Czech", "Hungarian", "Greek", "Belarusian", "Bulgarian", "Hebrew", "Finnish",
|
15 |
+
"Slovak", "Norwegian", "Danish", "Sinhala", "Croatian", "Lithuanian", "Slovenian", "Latvian",
|
16 |
+
"Estonian", "Armenian", "Malayalam", "Georgian", "Mongolian", "Afrikaans", "Nepali", "Pashto",
|
17 |
+
"Punjabi", "Kurdish", "Kyrgyz", "Somali", "Albanian", "Icelandic", "Basque", "Luxembourgish",
|
18 |
+
"Macedonian", "Maltese", "Hawaiian", "Yoruba", "Maori", "Zulu", "Welsh", "Swahili", "Haitian Creole",
|
19 |
+
"Lao", "Amharic", "Khmer", "Javanese", "Kazakh", "Malagasy", "Sindhi", "Sundanese", "Tajik", "Xhosa",
|
20 |
+
"Yiddish", "Bosnian", "Cebuano", "Chichewa", "Corsican", "Esperanto", "Frisian", "Galician", "Hausa",
|
21 |
+
"Hmong", "Igbo", "Irish", "Kinyarwanda", "Latin", "Samoan", "Scots Gaelic", "Sesotho", "Shona",
|
22 |
+
"Sotho", "Swedish", "Uyghur"
|
23 |
+
]
|
24 |
+
|
25 |
+
tokenizer = AutoTokenizer.from_pretrained("aixsatoshi/Honyaku-13b")
|
26 |
+
model = AutoModelForCausalLM.from_pretrained("aixsatoshi/Honyaku-13b", torch_dtype=torch.float16)
|
27 |
+
#model = model.to('cuda:0')
|
28 |
+
|
29 |
+
class StopOnTokens(StoppingCriteria):
|
30 |
+
def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor, **kwargs) -> bool:
|
31 |
+
stop_ids = [2]
|
32 |
+
for stop_id in stop_ids:
|
33 |
+
if input_ids[0][-1] == stop_id:
|
34 |
+
return True
|
35 |
+
return False
|
36 |
+
|
37 |
+
@spaces.GPU
|
38 |
+
def predict(message, history, tokens, temperature, language):
|
39 |
+
tag = "<" + language.lower() + ">"
|
40 |
+
history_transformer_format = history + [[message, ""]]
|
41 |
+
stop = StopOnTokens()
|
42 |
+
|
43 |
+
messages = "".join(["".join(["\n<english>:"+item[0]+"</english>\n", tag+item[1]])
|
44 |
+
for item in history_transformer_format])
|
45 |
+
|
46 |
+
model_inputs = tokenizer([messages], return_tensors="pt").to("cuda")
|
47 |
+
streamer = TextIteratorStreamer(tokenizer, timeout=10., skip_prompt=True, skip_special_tokens=True)
|
48 |
+
generate_kwargs = dict(
|
49 |
+
model_inputs,
|
50 |
+
streamer=streamer,
|
51 |
+
max_new_tokens=int(tokens),
|
52 |
+
temperature=float(temperature),
|
53 |
+
do_sample=True,
|
54 |
+
top_p=0.95,
|
55 |
+
top_k=20,
|
56 |
+
repetition_penalty=1.15,
|
57 |
+
num_beams=1,
|
58 |
+
stopping_criteria=StoppingCriteriaList([stop])
|
59 |
+
)
|
60 |
+
t = Thread(target=model.generate, kwargs=generate_kwargs)
|
61 |
+
t.start()
|
62 |
+
|
63 |
+
partial_message = ""
|
64 |
+
for new_token in streamer:
|
65 |
+
if new_token != '<':
|
66 |
+
partial_message += new_token
|
67 |
+
yield partial_message
|
68 |
+
|
69 |
+
# Gradioインタフェースの設定
|
70 |
demo = gr.ChatInterface(
|
71 |
+
fn=predict,
|
72 |
+
title="Honyaku-13b webui",
|
73 |
+
description="Translate using Honyaku-7b model",
|
74 |
additional_inputs=[
|
75 |
+
gr.Slider(100, 4096, value=1000, label="Tokens"),
|
76 |
+
gr.Slider(0.0, 1.0, value=0.3, label="Temperature"),
|
77 |
+
gr.Dropdown(choices=languages, value="Japanese", label="Language")
|
78 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
)
|
80 |
|
81 |
+
demo.queue().launch()
|
|
|
|