aixsatoshi commited on
Commit
d851055
1 Parent(s): c0dc977

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -58
app.py CHANGED
@@ -1,63 +1,81 @@
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
3
-
4
- """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
- """
7
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
-
9
-
10
- def respond(
11
- message,
12
- history: list[tuple[str, str]],
13
- system_message,
14
- max_tokens,
15
- temperature,
16
- top_p,
17
- ):
18
- messages = [{"role": "system", "content": system_message}]
19
-
20
- for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
25
-
26
- messages.append({"role": "user", "content": message})
27
-
28
- response = ""
29
-
30
- for message in client.chat_completion(
31
- messages,
32
- max_tokens=max_tokens,
33
- stream=True,
34
- temperature=temperature,
35
- top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
38
-
39
- response += token
40
- yield response
41
-
42
- """
43
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
44
- """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
  demo = gr.ChatInterface(
46
- respond,
 
 
47
  additional_inputs=[
48
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
49
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
50
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
51
- gr.Slider(
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()