PunGrumpy commited on
Commit
a07e90e
·
1 Parent(s): 4e79682

✨ feature: redirect on `/` to `/docs`

Browse files
Files changed (3) hide show
  1. .gitignore +6 -0
  2. app.py +15 -12
  3. requirements.txt +1 -0
.gitignore ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ # Environment
2
+ .venv
3
+ .env
4
+
5
+ # Cache
6
+ __pycache__/
app.py CHANGED
@@ -1,28 +1,31 @@
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
  # This function will be able to generate text
9
  # given an input.
10
- pipe = pipeline("text2text-generation",
11
- model="google/flan-t5-small")
12
-
13
- # Define a function to handle the GET request at `/generate`
14
- # The generate() function is defined as a FastAPI route that takes a
15
- # string parameter called text. The function generates text based on the # input using the pipeline() object, and returns a JSON response
16
- # containing the generated text under the key "output"
17
  @app.get("/")
 
 
 
 
 
 
 
 
18
  def generate(text: str):
19
  """
20
  Using the text2text-generation pipeline from `transformers`, generate text
21
  from the given input text. The model used is `google/flan-t5-small`, which
22
  can be found [here](<https://huggingface.co/google/flan-t5-small>).
23
  """
24
- # Use the pipeline to generate text from the given input text
25
  output = pipe(text)
26
-
27
- # Return the generated text in a JSON response
28
  return {"output": output[0]["generated_text"]}
 
1
  from fastapi import FastAPI
2
+ from fastapi.responses import RedirectResponse
3
  from transformers import pipeline
4
+
5
  # Create a new FastAPI app instance
6
  app = FastAPI()
7
+
8
  # Initialize the text generation pipeline
9
  # This function will be able to generate text
10
  # given an input.
11
+ pipe = pipeline("text2text-generation", model="google/flan-t5-small")
12
+
13
+
 
 
 
 
14
  @app.get("/")
15
+ def index():
16
+ """
17
+ Documentation for the API.
18
+ """
19
+ return RedirectResponse(url="/docs")
20
+
21
+
22
+ @app.get("/generate")
23
  def generate(text: str):
24
  """
25
  Using the text2text-generation pipeline from `transformers`, generate text
26
  from the given input text. The model used is `google/flan-t5-small`, which
27
  can be found [here](<https://huggingface.co/google/flan-t5-small>).
28
  """
 
29
  output = pipe(text)
30
+
 
31
  return {"output": output[0]["generated_text"]}
requirements.txt CHANGED
@@ -3,4 +3,5 @@ requests==2.27.*
3
  uvicorn[standard]==0.17.*
4
  sentencepiece==0.1.*
5
  torch==1.11.*
 
6
  transformers==4.*
 
3
  uvicorn[standard]==0.17.*
4
  sentencepiece==0.1.*
5
  torch==1.11.*
6
+ # torch==2.1.*
7
  transformers==4.*