Philippe Kaplan
commited on
Commit
·
f156ceb
1
Parent(s):
e1705ef
switch to fast API and mistral
Browse files
app.py
CHANGED
@@ -1,74 +1,54 @@
|
|
1 |
-
#
|
2 |
-
|
3 |
-
|
4 |
-
from
|
5 |
-
import
|
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 |
-
messages.append({"role": "assistant", "content": val[1]})
|
37 |
-
|
38 |
-
messages.append({"role": "user", "content": message})
|
39 |
-
|
40 |
-
response = ""
|
41 |
-
|
42 |
-
for message in client.chat.completions.create(
|
43 |
-
model="meta-llama/Meta-Llama-3.1-70B-Instruct",
|
44 |
-
max_tokens=max_tokens,
|
45 |
-
stream=True,
|
46 |
temperature=temperature,
|
|
|
47 |
top_p=top_p,
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
value=0.95,
|
65 |
-
step=0.05,
|
66 |
-
label="Top-P",
|
67 |
-
),
|
68 |
-
|
69 |
-
],
|
70 |
-
css=css,
|
71 |
-
theme="allenai/gradio-theme",
|
72 |
-
)
|
73 |
-
if __name__ == "__main__":
|
74 |
-
demo.launch()
|
|
|
1 |
+
# from https://huggingface.co/spaces/iiced/mixtral-46.7b-fastapi/blob/main/main.py
|
2 |
+
from fastapi import FastAPI
|
3 |
+
from pydantic import BaseModel
|
4 |
+
from huggingface_hub import InferenceClient
|
5 |
+
import uvicorn
|
6 |
+
|
7 |
+
|
8 |
+
app = FastAPI()
|
9 |
+
|
10 |
+
client = InferenceClient("mistralai/Mistral-7B-Instruct-v0.3")
|
11 |
+
|
12 |
+
class Item(BaseModel):
|
13 |
+
prompt: str
|
14 |
+
history: list
|
15 |
+
system_prompt: str
|
16 |
+
temperature: float = 0.0
|
17 |
+
max_new_tokens: int = 1048
|
18 |
+
top_p: float = 0.15
|
19 |
+
repetition_penalty: float = 1.0
|
20 |
+
|
21 |
+
def format_prompt(message, history):
|
22 |
+
prompt = "<s>"
|
23 |
+
for user_prompt, bot_response in history:
|
24 |
+
prompt += f"[INST] {user_prompt} [/INST]"
|
25 |
+
prompt += f" {bot_response}</s> "
|
26 |
+
prompt += f"[INST] {message} [/INST]"
|
27 |
+
return prompt
|
28 |
+
|
29 |
+
def generate(item: Item):
|
30 |
+
temperature = float(item.temperature)
|
31 |
+
if temperature < 1e-2:
|
32 |
+
temperature = 1e-2
|
33 |
+
top_p = float(item.top_p)
|
34 |
+
|
35 |
+
generate_kwargs = dict(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
temperature=temperature,
|
37 |
+
max_new_tokens=item.max_new_tokens,
|
38 |
top_p=top_p,
|
39 |
+
repetition_penalty=item.repetition_penalty,
|
40 |
+
do_sample=True,
|
41 |
+
seed=42,
|
42 |
+
)
|
43 |
+
|
44 |
+
formatted_prompt = format_prompt(f"{item.system_prompt}, {item.prompt}", item.history)
|
45 |
+
stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
|
46 |
+
output = ""
|
47 |
+
|
48 |
+
for response in stream:
|
49 |
+
output += response.token.text
|
50 |
+
return output
|
51 |
+
|
52 |
+
@app.post("/generate/")
|
53 |
+
async def generate_text(item: Item):
|
54 |
+
return {"response": generate(item)}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|