luck210 commited on
Commit
e059b03
·
verified ·
1 Parent(s): 5578867

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -19
app.py CHANGED
@@ -1,25 +1,25 @@
1
- from fastapi import FastAPI, HTTPException
2
- from pydantic import BaseModel
3
  from transformers import pipeline
4
 
5
- app = FastAPI()
 
6
 
7
- # Charger le modèle de traduction
8
- translator = pipeline("translation", model="Helsinki-NLP/opus-mt-en-fr")
9
 
10
- class TranslationRequest(BaseModel):
11
- text: str
12
-
13
- @app.post("/translate/")
14
- async def translate(request: TranslationRequest):
15
- if not request.text:
16
- raise HTTPException(status_code=400, detail="Le texte ne peut pas être vide.")
17
- try:
18
- result = translator(request.text)
19
- return {"translated_text": result[0]['translation_text']}
20
- except Exception as e:
21
- raise HTTPException(status_code=500, detail=str(e))
22
 
23
  @app.get("/")
24
- async def root():
25
- return {"message": "Bienvenue sur l'API de traduction avec FastAPI 🚀"}
 
 
 
 
 
 
 
 
 
 
 
 
 
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
8
+ pipe = pipeline("text2text-generation", model="google/flan-t5-small")
9
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
  @app.get("/")
12
+ def home():
13
+ return {"message":"Hello World"}
14
+
15
+ # Define a function to handle the GET request at `/generate`
16
+
17
+
18
+ @app.get("/generate")
19
+ def generate(text:str):
20
+ ## use the pipeline to generate text from given input text
21
+ output=pipe(text)
22
+
23
+ ## return the generate text in Json reposne
24
+ return {"output":output[0]['generated_text']}
25
+