Spaces:
Runtime error
Runtime error
T.Masuda
commited on
Commit
•
4a8f3cf
1
Parent(s):
225b381
create app
Browse files- app.py +49 -0
- requirements.txt +6 -0
app.py
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
4 |
+
from datetime import datetime
|
5 |
+
|
6 |
+
print('{}:loading...'.format(datetime.now().strftime('%Y-%m-%d %H:%M:%S')))
|
7 |
+
|
8 |
+
tokenizer = AutoTokenizer.from_pretrained('microsoft/Phi-3-mini-128k-instruct')
|
9 |
+
model = AutoModelForCausalLM.from_pretrained('microsoft/Phi-3-mini-128k-instruct', torch_dtype='auto', trust_remote_code=True)
|
10 |
+
|
11 |
+
if torch.cuda.is_available():
|
12 |
+
model.half()
|
13 |
+
model = model.to('cuda')
|
14 |
+
|
15 |
+
generator = pipeline('text-generation', model=model, tokenizer=tokenizer, device=model.device)
|
16 |
+
print('{}:done.'.format(datetime.now().strftime('%Y-%m-%d %H:%M:%S')))
|
17 |
+
|
18 |
+
def generate(input_text, maxlen):
|
19 |
+
messages = [
|
20 |
+
{'role': 'user', 'content': input_text}
|
21 |
+
]
|
22 |
+
output = generator(
|
23 |
+
messages,
|
24 |
+
max_new_tokens=maxlen,
|
25 |
+
return_full_text=False,
|
26 |
+
do_sample=True,
|
27 |
+
temperature=0.7,
|
28 |
+
)
|
29 |
+
generated_text = output[0]['generated_text']
|
30 |
+
return generated_text
|
31 |
+
|
32 |
+
with gr.Blocks(title='phi3 demo') as app:
|
33 |
+
gr.Markdown('# Phi3 Demo')
|
34 |
+
|
35 |
+
chatbot = gr.Chatbot(label='answer')
|
36 |
+
msg = gr.Textbox(label='question')
|
37 |
+
maxlen = gr.Slider(minimum=30, maximum=100, value=30, step=1, label='max length')
|
38 |
+
clear = gr.ClearButton([msg, chatbot])
|
39 |
+
|
40 |
+
def respond(message, maxlen, chat_history):
|
41 |
+
if message == '':
|
42 |
+
return '', chat_history
|
43 |
+
bot_message = generate(message, maxlen)
|
44 |
+
chat_history.append((message, bot_message))
|
45 |
+
return '', chat_history
|
46 |
+
|
47 |
+
msg.submit(respond, [msg, maxlen, chatbot], [msg, chatbot], concurrency_limit=20)
|
48 |
+
|
49 |
+
app.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
torch
|
3 |
+
torchvision
|
4 |
+
torchaudio
|
5 |
+
transformers
|
6 |
+
sentencepiece
|