Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
| 3 |
+
import gradio as gr
|
| 4 |
+
|
| 5 |
+
torch.random.manual_seed(0)
|
| 6 |
+
|
| 7 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 8 |
+
"microsoft/Phi-3-mini-128k-instruct",
|
| 9 |
+
device_map="auto",
|
| 10 |
+
torch_dtype="auto",
|
| 11 |
+
trust_remote_code=True,
|
| 12 |
+
low_cpu_mem_usage=True,
|
| 13 |
+
)
|
| 14 |
+
tokenizer = AutoTokenizer.from_pretrained("microsoft/Phi-3-mini-128k-instruct")
|
| 15 |
+
|
| 16 |
+
messages = [
|
| 17 |
+
{"role": "user", "content": "Can you provide ways to eat combinations of bananas and dragonfruits?"},
|
| 18 |
+
{"role": "assistant", "content": "Sure! Here are some ways to eat bananas and dragonfruits together: 1. Banana and dragonfruit smoothie: Blend bananas and dragonfruits together with some milk and honey. 2. Banana and dragonfruit salad: Mix sliced bananas and dragonfruits together with some lemon juice and honey."},
|
| 19 |
+
{"role": "user", "content": "What about solving an 2x + 3 = 7 equation?"},
|
| 20 |
+
]
|
| 21 |
+
|
| 22 |
+
pipe = pipeline(
|
| 23 |
+
"text-generation",
|
| 24 |
+
model=model,
|
| 25 |
+
tokenizer=tokenizer,
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
generation_args = {
|
| 29 |
+
"max_new_tokens": 500,
|
| 30 |
+
"return_full_text": False,
|
| 31 |
+
"temperature": 0.2,
|
| 32 |
+
"do_sample": True,
|
| 33 |
+
}
|
| 34 |
+
def phi3_fun(message,chat_history):
|
| 35 |
+
messages=[
|
| 36 |
+
{"role": "user", "content": message},
|
| 37 |
+
]
|
| 38 |
+
output = pipe(messages, **generation_args)
|
| 39 |
+
respond = output[0]['generated_text']
|
| 40 |
+
return respond
|
| 41 |
+
|
| 42 |
+
title = "Phi-3 "
|
| 43 |
+
examples = [
|
| 44 |
+
'How are You?',
|
| 45 |
+
"talk about your self",
|
| 46 |
+
]
|
| 47 |
+
gr.ChatInterface(
|
| 48 |
+
fn=phi3_fun,
|
| 49 |
+
title =title,
|
| 50 |
+
examples = examples
|
| 51 |
+
).launch(debug=True)
|
| 52 |
+
|
| 53 |
+
# output = pipe(messages, **generation_args)
|
| 54 |
+
# print(output[0]['generated_text'])
|