haroldd commited on
Commit
426b05b
ยท
verified ยท
1 Parent(s): 7c5e848

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -9
app.py CHANGED
@@ -1,22 +1,28 @@
1
  from fastapi import FastAPI
2
  from transformers import pipeline
3
 
4
- # ์ƒˆ๋กœ์šด FastAPI ์•ฑ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•œ๋‹ค
5
  app = FastAPI()
6
 
7
- # ํ…์ŠคํŠธ ์ƒ์„ฑ ํŒŒ์ดํ”„๋ผ์ธ์„ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค
8
- pipe = pipeline("text2text-generation", model="google/flan-t5-small")
 
 
 
9
 
10
- # `/generate`์—์„œ GET ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•œ๋‹ค
 
 
 
11
  @app.get("/generate")
12
  def generate(text: str):
13
  """
14
- transformers์˜ text2text-generation ํŒŒ์ดํ”„๋ผ์ธ์„ ์‚ฌ์šฉํ•˜์—ฌ
15
- ์ฃผ์–ด์ง„ ์ž…๋ ฅ ํ…์ŠคํŠธ์—์„œ ํ…์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.
16
- ์‚ฌ์šฉ๋œ ๋ชจ๋ธ์€ `google/flan-t5-small`์ด๋‹ค.
17
  """
18
- # ํŒŒ์ดํ”„๋ผ์ธ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ฃผ์–ด์ง„ ์ž…๋ ฅ ํ…์ŠคํŠธ์—์„œ ํ…์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค
19
  output = pipe(text)
20
 
21
- # ์ƒ์„ฑ๋œ ํ…์ŠคํŠธ๋ฅผ JSON ์‘๋‹ต์œผ๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค
22
  return {"output": output[0]["generated_text"]}
 
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("/generate")
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"]}