arshadrana commited on
Commit
9ad2b79
·
verified ·
1 Parent(s): 5f58f15

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -18
app.py CHANGED
@@ -1,23 +1,18 @@
1
- import gradio as gr
2
  from transformers import pipeline
 
 
3
 
4
- # Load a pre-trained text-generation model
5
- generator = pipeline('text-generation', model='gpt2')
6
 
7
- # Function to process the input text
8
- def generate_text(input_text):
9
- # Generate text using the model
10
- result = generator(input_text, max_length=50, num_return_sequences=1)
11
- return result[0]['generated_text']
12
 
13
- # Set up Gradio interface
14
- iface = gr.Interface(
15
- fn=generate_text, # Function to call
16
- inputs="text", # Input type
17
- outputs="text", # Output type
18
- title="Text Generation API", # Title of the API
19
- description="Provide text as input, and the model will generate some text.",
20
- )
21
 
22
- # Launch the interface
23
- iface.launch()
 
 
 
 
 
1
  from transformers import pipeline
2
+ from fastapi import FastAPI
3
+ from pydantic import BaseModel
4
 
5
+ app = FastAPI()
 
6
 
7
+ # Load the text generation pipeline from Hugging Face
8
+ generator = pipeline('text-generation', model='gpt2')
 
 
 
9
 
10
+ # Define the request body
11
+ class TextInput(BaseModel):
12
+ input_text: str
 
 
 
 
 
13
 
14
+ @app.post("/generate")
15
+ def generate_text(text_input: TextInput):
16
+ # Generate text from the model based on the input
17
+ generated = generator(text_input.input_text, max_length=100, num_return_sequences=1)
18
+ return {"generated_text": generated[0]['generated_text']}