sailormars18 commited on
Commit
fa65b7a
·
1 Parent(s): 959dfde

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -6
app.py CHANGED
@@ -1,9 +1,10 @@
1
  import subprocess
2
- subprocess.run(["pip", "install","gradio","torch","transformers"])
3
  import re
4
  import gradio as gr
5
  import torch
6
  import transformers
 
7
 
8
  import json
9
  from transformers import GPT2LMHeadModel, GPT2Tokenizer
@@ -13,12 +14,18 @@ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
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_url = "https://huggingface.co/spaces/sailormars18/Yelp-reviews-usingGPT2/blob/main/pytorch_model.bin"
17
- config_url = "https://huggingface.co/spaces/sailormars18/Yelp-reviews-usingGPT2/blob/main/config.json"
18
- generation_config_url = "https://huggingface.co/spaces/sailormars18/Yelp-reviews-usingGPT2/blob/main/generation_config.json"
19
 
20
- # Load the model from the Hugging Face space
21
- model = transformers.GPT2LMHeadModel.from_pretrained(model_url, config=config_url).to(device)
 
 
 
 
 
 
 
22
 
23
  # Load the tokenizer from the Hugging Face space
24
  tokenizer = transformers.GPT2Tokenizer.from_pretrained('gpt2')
@@ -63,6 +70,13 @@ def generate_text(prompt, length=100, theme=None, **kwargs):
63
 
64
  return generated_text
65
 
 
 
 
 
 
 
 
66
  # Define a Gradio interface for the generate_text function
67
  iface = gr.Interface(
68
  fn=generate_text,
 
1
  import subprocess
2
+ subprocess.run(["pip", "install", "gradio", "torch", "transformers"])
3
  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
  # 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/resolve/main/pytorch_model.bin"
18
+ config_url = "https://huggingface.co/spaces/sailormars18/Yelp-reviews-usingGPT2/resolve/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')
 
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,