misukisu commited on
Commit
2dad476
·
verified ·
1 Parent(s): 2a29128

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -1
app.py CHANGED
@@ -1,11 +1,30 @@
1
  from fastapi import FastAPI
2
  from pydantic import BaseModel
 
3
 
4
  app = FastAPI()
5
 
 
 
 
 
 
6
  class Req(BaseModel):
7
  message: str
8
 
9
  @app.post("/chat")
10
  def chat(req: Req):
11
- return {"reply": f"AI says: {req.message}"}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  from fastapi import FastAPI
2
  from pydantic import BaseModel
3
+ from transformers import pipeline
4
 
5
  app = FastAPI()
6
 
7
+ pipe = pipeline(
8
+ "text-generation",
9
+ model="TinyLlama/TinyLlama-1.1B-Chat-v1.0"
10
+ )
11
+
12
  class Req(BaseModel):
13
  message: str
14
 
15
  @app.post("/chat")
16
  def chat(req: Req):
17
+ prompt = f"<s>[INST] {req.message} [/INST]"
18
+
19
+ out = pipe(
20
+ prompt,
21
+ max_new_tokens=100,
22
+ temperature=0.7
23
+ )
24
+
25
+ text = out[0]["generated_text"]
26
+
27
+ # remove prompt from output
28
+ reply = text.split("[/INST]")[-1].strip()
29
+
30
+ return {"reply": reply}