DataChem commited on
Commit
4468cfe
·
verified ·
1 Parent(s): d42300f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -1
app.py CHANGED
@@ -1,7 +1,30 @@
1
- from fastapi import FastAPI
 
 
2
 
3
  app = FastAPI()
4
 
 
 
 
 
 
5
  @app.get("/")
6
  def read_root():
7
  return {"Hello": "World"}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, Request
2
+ from transformers import AutoModelForCausalLM, AutoTokenizer
3
+ import torch
4
 
5
  app = FastAPI()
6
 
7
+ # Load the model and tokenizer
8
+ model_name = "EleutherAI/gpt-neo-1.3B" # Replace with your desired model
9
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
10
+ model = AutoModelForCausalLM.from_pretrained(model_name)
11
+
12
  @app.get("/")
13
  def read_root():
14
  return {"Hello": "World"}
15
+
16
+ @app.post("/predict")
17
+ async def predict(request: Request):
18
+ data = await request.json()
19
+ prompt = data.get("prompt", "")
20
+ if not prompt:
21
+ return {"error": "Prompt is required"}
22
+
23
+ # Tokenize the input
24
+ inputs = tokenizer(prompt, return_tensors="pt").to("cpu") # Use "cuda" if GPU is enabled
25
+
26
+ # Generate tokens
27
+ outputs = model.generate(inputs.input_ids, max_length=40, num_return_sequences=1)
28
+ response = tokenizer.decode(outputs[0], skip_special_tokens=True)
29
+
30
+ return {"response": response}