Update app.py
Browse files
app.py
CHANGED
|
@@ -1,43 +1,30 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 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 |
-
|
| 21 |
-
|
| 22 |
-
messages.append({"role": "user", "content": val[0]})
|
| 23 |
-
if val[1]:
|
| 24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
| 25 |
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
|
| 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 |
"""
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from fastapi import FastAPI
|
| 3 |
+
from pydantic import BaseModel
|
| 4 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 5 |
+
import torch
|
| 6 |
|
| 7 |
+
# Load model and tokenizer
|
| 8 |
+
tokenizer = AutoTokenizer.from_pretrained("cognitivecomputations/Dolphin3.0-Mistral-24B")
|
| 9 |
+
model = AutoModelForCausalLM.from_pretrained("cognitivecomputations/Dolphin3.0-Mistral-24B", torch_dtype=torch.float16).cuda()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
# FastAPI app
|
| 12 |
+
app = FastAPI()
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
+
# Request Body
|
| 15 |
+
class InputText(BaseModel):
|
| 16 |
+
prompt: str
|
| 17 |
+
max_length: int = 100
|
| 18 |
|
| 19 |
+
@app.post("/generate")
|
| 20 |
+
async def generate_text(input_data: InputText):
|
| 21 |
+
inputs = tokenizer(input_data.prompt, return_tensors="pt").to("cuda")
|
| 22 |
+
output = model.generate(**inputs, max_length=input_data.max_length)
|
| 23 |
+
generated_text = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 24 |
+
return {"response": generated_text}
|
| 25 |
|
| 26 |
+
# Run the server using: uvicorn app:app --host 0.0.0.0 --port 8000
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
|
|
|
|
|
|
| 28 |
|
| 29 |
|
| 30 |
"""
|