File size: 1,060 Bytes
dcdccdb
 
ab580b5
dcdccdb
 
 
 
 
 
 
 
ab580b5
 
dcdccdb
ab580b5
dcdccdb
ab580b5
dcdccdb
 
ab580b5
dcdccdb
ab580b5
dcdccdb
 
 
ab580b5
dcdccdb
ab580b5
dcdccdb
 
 
 
 
ab580b5
dcdccdb
ab580b5
 
dcdccdb
 
 
 
 
ab580b5
 
dcdccdb
ab580b5
dcdccdb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# -*- coding: utf-8 -*-
"""app.ipynb

Automatically generated by Colab.

Original file is located at
    https://colab.research.google.com/drive/1qNBkOEPBOkXJ0zcGdwQmdS7bt5zxjpIr

##Creating app.py

###Installing Dependencies
"""

!pip install gradio transformers torch

"""###Importing Dependencies"""

import gradio as gr
from transformers import AutoModelForCausalLM, AutoTokenizer

"""###Loading the model and tokenizer"""

model_name = "gpt2"
model = AutoModelForCausalLM.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name)

"""###Defining the prediction function"""

def generate_text(prompt):
    inputs = tokenizer(prompt, return_tensors="pt")
    outputs = model.generate(**inputs, max_length=100)
    generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
    return generated_text

"""###Creating the Gradio interface

"""

api = gr.Interface(
    fn=generate_text,
    inputs=gr.Textbox(label="Input Prompt"),
    outputs=gr.Textbox(label="Generated Text"),
)

"""###Launching the API"""

api.launch()