Spaces:
Sleeping
Sleeping
Chengxb888
commited on
Commit
•
1856062
1
Parent(s):
9c0850a
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI
|
2 |
+
import torch
|
3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
4 |
+
|
5 |
+
app = FastAPI()
|
6 |
+
|
7 |
+
@app.get("/")
|
8 |
+
def greet_json():
|
9 |
+
return {"Hello": "World!"}
|
10 |
+
|
11 |
+
@app.get("/hello/{msg}")
|
12 |
+
async def say_hello(msg: str):
|
13 |
+
torch.random.manual_seed(0)
|
14 |
+
model = AutoModelForCausalLM.from_pretrained(
|
15 |
+
"microsoft/Phi-3-mini-128k-instruct",
|
16 |
+
device_map="cpu",
|
17 |
+
torch_dtype="auto",
|
18 |
+
trust_remote_code=True,
|
19 |
+
)
|
20 |
+
|
21 |
+
tokenizer = AutoTokenizer.from_pretrained("microsoft/Phi-3-mini-128k-instruct")
|
22 |
+
|
23 |
+
messages = [
|
24 |
+
{"role": "system", "content": "You are a helpful AI assistant."},
|
25 |
+
{"role": "user", "content": "Can you provide ways to eat combinations of bananas and dragonfruits?"},
|
26 |
+
{"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."},
|
27 |
+
{"role": "user", "content": msg},
|
28 |
+
]
|
29 |
+
|
30 |
+
pipe = pipeline(
|
31 |
+
"text-generation",
|
32 |
+
model=model,
|
33 |
+
tokenizer=tokenizer,
|
34 |
+
)
|
35 |
+
|
36 |
+
generation_args = {
|
37 |
+
"max_new_tokens": 500,
|
38 |
+
"return_full_text": False,
|
39 |
+
"temperature": 0.0,
|
40 |
+
"do_sample": False,
|
41 |
+
}
|
42 |
+
|
43 |
+
output = pipe(messages, **generation_args)
|
44 |
+
return {"message": output[0]['generated_text']}
|