arjunkmoorthy commited on
Commit
fb91760
·
verified ·
1 Parent(s): 370e742

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -0
app.py ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI
2
+ from pydantic import BaseModel
3
+ from transformers import AutoTokenizer, AutoModelForCausalLM
4
+ import torch
5
+
6
+ app = FastAPI()
7
+
8
+ # Load ChatDoctor model
9
+ model_id = "z1111/ChatDoctor"
10
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
11
+ model = AutoModelForCausalLM.from_pretrained(
12
+ model_id,
13
+ torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32
14
+ )
15
+
16
+ class ChatInput(BaseModel):
17
+ message: str
18
+
19
+ @app.post("/chat")
20
+ async def chat_handler(input: ChatInput):
21
+ inputs = tokenizer(input.message, return_tensors="pt")
22
+ outputs = model.generate(**inputs, max_new_tokens=200)
23
+ response = tokenizer.decode(outputs[0], skip_special_tokens=True)
24
+ return {"response": response}