harry85 commited on
Commit
8290afa
·
verified ·
1 Parent(s): ff3d29a

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -27
app.py CHANGED
@@ -7,7 +7,6 @@ from pydantic import BaseModel
7
  from fastapi import FastAPI
8
  # Import the required library
9
  from transformers import pipeline
10
- # Load the tokenizer and model
11
 
12
  # Initialize the FastAPI app
13
  app = FastAPI(docs_url="/")
@@ -25,31 +24,29 @@ def greet_json():
25
  @app.post("/generatetext")
26
  def get_response(request: RequestModel):
27
  # Define the task and model
28
- task = "text-generation"
29
- model_name = "gpt2"
30
-
31
- # Define the input text, maximum output length, and the number of return sequences
32
- input_text = "he draw to the town "
33
- max_output_length = 50
34
- num_of_return_sequences = 1
35
-
36
- # Initialize the text generation pipeline
37
- text_generator = pipeline(
38
- task,
39
- model=model_name
40
- )
41
-
42
- # Generate text sequences
43
- generated_texts = text_generator(
44
- input_text,
45
- max_length=max_output_length,
46
- num_return_sequences=num_of_return_sequences
47
- )
48
-
49
- # Print the generated text sequences
50
- for i, text in enumerate(generated_texts):
51
- print(f"Generated Text {1}: {text['generated_text']}")
52
-
53
- return {"generated_text": text['generated_text']}
54
 
55
  # To run the FastAPI app, use the command: uvicorn <filename>:app --reload
 
7
  from fastapi import FastAPI
8
  # Import the required library
9
  from transformers import pipeline
 
10
 
11
  # Initialize the FastAPI app
12
  app = FastAPI(docs_url="/")
 
24
  @app.post("/generatetext")
25
  def get_response(request: RequestModel):
26
  # Define the task and model
27
+ task = "text-generation"
28
+ model_name = "gpt2"
29
+
30
+ # Define the input text, maximum output length, and the number of return sequences
31
+ input_text = request.input
32
+ max_output_length = 50
33
+ num_of_return_sequences = 1
34
+
35
+ # Initialize the text generation pipeline
36
+ text_generator = pipeline(
37
+ task,
38
+ model=model_name
39
+ )
40
+
41
+ # Generate text sequences
42
+ generated_texts = text_generator(
43
+ input_text,
44
+ max_length=max_output_length,
45
+ num_return_sequences=num_of_return_sequences
46
+ )
47
+
48
+ # Extract and return the generated text
49
+ generated_text = generated_texts[0]['generated_text']
50
+ return {"generated_text": generated_text}
 
 
51
 
52
  # To run the FastAPI app, use the command: uvicorn <filename>:app --reload