barathm111 commited on
Commit
95b255d
·
verified ·
1 Parent(s): 6f92cbb

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -10
app.py CHANGED
@@ -1,7 +1,8 @@
1
- from fastapi import FastAPI
 
2
  from transformers import pipeline
3
 
4
- # Create a new FASTAPI app instance
5
  app = FastAPI()
6
 
7
  # Initialize the text generation pipeline
@@ -11,11 +12,16 @@ pipe = pipeline("text-generation", model="defog/sqlcoder-7b-2")
11
  def home():
12
  return {"message": "Hello World"}
13
 
14
- # Define a function to handle the GET request at '/generate'
15
- @app.get("/generate")
16
- def generate(text: str):
17
- output = pipe(text, max_new_tokens=50)
18
- # Use the pipeline to generate text from the given input text
19
-
20
- # Return the generated text in JSON response
21
- return {"output": output[0]['generated_text']}
 
 
 
 
 
 
1
+ from fastapi import FastAPI, HTTPException
2
+ from pydantic import BaseModel
3
  from transformers import pipeline
4
 
5
+ # Create a new FastAPI app instance
6
  app = FastAPI()
7
 
8
  # Initialize the text generation pipeline
 
12
  def home():
13
  return {"message": "Hello World"}
14
 
15
+ # Define a function to handle the POST request at '/generate'
16
+ @app.post("/generate")
17
+ def generate(request: dict):
18
+ try:
19
+ text = request.get('text')
20
+ if not text:
21
+ raise HTTPException(status_code=400, detail="Text field is required")
22
+
23
+ output = pipe(text, max_new_tokens=50)
24
+ # Return the generated text in JSON response
25
+ return {"output": output[0]['generated_text']}
26
+ except Exception as e:
27
+ raise HTTPException(status_code=500, detail=str(e))