Spaces:
Sleeping
Sleeping
Download app.py from Ankoorkashyap/Sentence_Completion: direct link, hf CLI and curl.
- Browser
- Download file 835 Bytes
-
https://huggingface.co/spaces/Ankoorkashyap/Sentence_Completion/resolve/main/app.py
- Command line
-
hf download hf://spaces/Ankoorkashyap/Sentence_Completion/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Ankoorkashyap/Sentence_Completion/resolve/main/app.py
835 Bytes
| import gradio as gr | |
| from transformers import GPT2LMHeadModel, GPT2Tokenizer | |
| # Load the GPT-2 model and tokenizer | |
| model_name = "gpt2" | |
| model = GPT2LMHeadModel.from_pretrained(model_name) | |
| tokenizer = GPT2Tokenizer.from_pretrained(model_name) | |
| # Define the sentence completion function | |
| def complete_sentence(sentence): | |
| input_ids = tokenizer.encode(sentence, return_tensors="pt") | |
| output = model.generate(input_ids, max_length=50, num_return_sequences=1) | |
| completed_sentence = tokenizer.decode(output[0], skip_special_tokens=True) | |
| return completed_sentence | |
| # Create the Gradio interface | |
| iface = gr.Interface( | |
| fn=complete_sentence, | |
| inputs="text", | |
| outputs="text", | |
| title="Sentence Completion", | |
| description="Enter a sentence to complete", | |
| example="I love to" | |
| ) | |
| # Launch the Gradio interface | |
| if __name__ == "__main__": | |
| iface.launch() |