deepumanju commited on
Commit
f7739aa
1 Parent(s): 7d81b02

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +32 -31
main.py CHANGED
@@ -1,31 +1,32 @@
1
- from fastapi import FastAPI, HTTPException
2
- from pydantic import BaseModel
3
- from transformers import pipeline
4
-
5
- app = FastAPI()
6
-
7
- # Load the text-to-text generation pipeline
8
- pipe = pipeline("text2text-generation", model="google/flan-t5-small")
9
-
10
- # Define the input data model
11
- class GenerateRequest(BaseModel):
12
- text: str
13
-
14
- @app.get("/")
15
- def welcome():
16
- """
17
- Welcoming page.
18
- """
19
- return {"message": "Welcome to the Text-to-Text Generation API! Use the /generate endpoint to transform text."}
20
-
21
- @app.post("/generate")
22
- def generate_text(data: GenerateRequest):
23
- """
24
- Generate text from the input text using the text-to-text generation model.
25
- """
26
- try:
27
- # Perform text-to-text generation
28
- result = pipe(data.text, max_length=50, num_return_sequences=1)
29
- return {"input": data.text, "generated_text": result[0]["generated_text"]}
30
- except Exception as e:
31
- raise HTTPException(status_code=500, detail=str(e))
 
 
1
+ from fastapi import FastAPI, HTTPException
2
+ from pydantic import BaseModel
3
+ from transformers import pipeline
4
+
5
+ app = FastAPI()
6
+
7
+
8
+ # Load the text-to-text generation pipeline
9
+ pipe = pipeline("text2text-generation", model="google/flan-t5-small")
10
+
11
+ # Define the input data model
12
+ class GenerateRequest(BaseModel):
13
+ text: str
14
+
15
+ @app.get("/")
16
+ def welcome():
17
+ """
18
+ Welcoming page.
19
+ """
20
+ return {"message": "Welcome to the Text-to-Text Generation API! Use the /generate endpoint to transform text."}
21
+
22
+ @app.post("/generate")
23
+ def generate_text(data: GenerateRequest):
24
+ """
25
+ Generate text from the input text using the text-to-text generation model.
26
+ """
27
+ try:
28
+ # Perform text-to-text generation
29
+ result = pipe(data.text, max_length=50, num_return_sequences=1)
30
+ return {"input": data.text, "generated_text": result[0]["generated_text"]}
31
+ except Exception as e:
32
+ raise HTTPException(status_code=500, detail=str(e))