Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,31 +1,42 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
-
import
|
3 |
from transformers import GPT2Tokenizer, GPT2LMHeadModel
|
4 |
import os
|
5 |
|
|
|
6 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
7 |
|
8 |
# Load the tokenizer and model
|
9 |
tokenizer = GPT2Tokenizer.from_pretrained('gpt2', use_auth_token=HF_TOKEN)
|
10 |
model = GPT2LMHeadModel.from_pretrained('skylersterling/TopicGPT', use_auth_token=HF_TOKEN)
|
11 |
model.eval()
|
|
|
12 |
|
13 |
# Define the function that generates text from a prompt
|
14 |
-
def generate_text(prompt
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
|
|
|
|
31 |
interface.launch()
|
|
|
1 |
+
# Import the necessary libraries
|
2 |
import gradio as gr
|
3 |
+
import torch
|
4 |
from transformers import GPT2Tokenizer, GPT2LMHeadModel
|
5 |
import os
|
6 |
|
7 |
+
# Get the Hugging Face token from the environment variable
|
8 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
9 |
|
10 |
# Load the tokenizer and model
|
11 |
tokenizer = GPT2Tokenizer.from_pretrained('gpt2', use_auth_token=HF_TOKEN)
|
12 |
model = GPT2LMHeadModel.from_pretrained('skylersterling/TopicGPT', use_auth_token=HF_TOKEN)
|
13 |
model.eval()
|
14 |
+
model.to('cuda')
|
15 |
|
16 |
# Define the function that generates text from a prompt
|
17 |
+
def generate_text(prompt):
|
18 |
+
input_tokens = tokenizer.encode(prompt, return_tensors='pt')
|
19 |
+
input_tokens = input_tokens.to('cuda')
|
20 |
+
|
21 |
+
generated_tokens = []
|
22 |
|
23 |
+
for _ in range(80): # Adjust the range to control the number of tokens generated
|
24 |
+
with torch.no_grad():
|
25 |
+
outputs = model(input_tokens)
|
26 |
+
predictions = outputs.logits
|
27 |
+
next_token = torch.multinomial(torch.softmax(predictions[:, -1, :], dim=-1), 1)
|
28 |
+
|
29 |
+
generated_tokens.append(next_token.item())
|
30 |
+
input_tokens = torch.cat((input_tokens, next_token), dim=1)
|
31 |
+
|
32 |
+
decoded_token = tokenizer.decode(next_token.item())
|
33 |
+
# Print each token as it is generated
|
34 |
+
print(decoded_token, end='', flush=True)
|
35 |
+
|
36 |
+
# Decode the generated tokens to a string
|
37 |
+
generated_text = tokenizer.decode(generated_tokens, skip_special_tokens=True)
|
38 |
+
return generated_text
|
39 |
|
40 |
+
# Create a Gradio interface with a text input and a text output
|
41 |
+
interface = gr.Interface(fn=generate_text, inputs='text', outputs='text')
|
42 |
interface.launch()
|