sailormars18 commited on
Commit
2672584
·
1 Parent(s): f07e8e2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +4 -20
app.py CHANGED
@@ -4,7 +4,6 @@ import re
4
  import gradio as gr
5
  import torch
6
  import transformers
7
- import requests
8
 
9
  import json
10
  from transformers import GPT2LMHeadModel, GPT2Tokenizer
@@ -14,21 +13,13 @@ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
14
  # Define a function for generating text based on a prompt using the fine-tuned GPT-2 model and the tokenizer
15
  def generate_text(prompt, length=100, theme=None, **kwargs):
16
 
17
- model_url = "https://huggingface.co/spaces/sailormars18/Yelp-reviews-usingGPT2/blob/main/pytorch_model.bin"
18
- config_url = "https://huggingface.co/spaces/sailormars18/Yelp-reviews-usingGPT2/blob/main/config.json"
19
 
20
- # Download the model and configuration files
21
- model_path = "./pytorch_model.bin"
22
- config_path = "./config.json"
23
-
24
- download_file(model_url, model_path)
25
- download_file(config_url, config_path)
26
-
27
- # Load the model from the downloaded files
28
- model = transformers.GPT2LMHeadModel.from_pretrained(model_path, config=config_path).to(device)
29
 
30
  # Load the tokenizer from the Hugging Face space
31
- tokenizer = transformers.GPT2Tokenizer.from_pretrained('gpt2')
32
 
33
  # If a theme is specified, add it to the prompt as a prefix for a special token
34
  if theme:
@@ -70,13 +61,6 @@ def generate_text(prompt, length=100, theme=None, **kwargs):
70
 
71
  return generated_text
72
 
73
- def download_file(url, save_path):
74
- response = requests.get(url, stream=True)
75
- response.raise_for_status()
76
- with open(save_path, "wb") as f:
77
- for chunk in response.iter_content(chunk_size=8192):
78
- f.write(chunk)
79
-
80
  # Define a Gradio interface for the generate_text function
81
  iface = gr.Interface(
82
  fn=generate_text,
 
4
  import gradio as gr
5
  import torch
6
  import transformers
 
7
 
8
  import json
9
  from transformers import GPT2LMHeadModel, GPT2Tokenizer
 
13
  # Define a function for generating text based on a prompt using the fine-tuned GPT-2 model and the tokenizer
14
  def generate_text(prompt, length=100, theme=None, **kwargs):
15
 
16
+ model_name = "sailormars18/Yelp-reviews-usingGPT2"
 
17
 
18
+ # Load the model from the Hugging Face space
19
+ model = GPT2LMHeadModel.from_pretrained(model_name).to(device)
 
 
 
 
 
 
 
20
 
21
  # Load the tokenizer from the Hugging Face space
22
+ tokenizer = GPT2Tokenizer.from_pretrained(model_name)
23
 
24
  # If a theme is specified, add it to the prompt as a prefix for a special token
25
  if theme:
 
61
 
62
  return generated_text
63
 
 
 
 
 
 
 
 
64
  # Define a Gradio interface for the generate_text function
65
  iface = gr.Interface(
66
  fn=generate_text,