Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,22 +1,32 @@
|
|
1 |
import gradio as gr
|
2 |
-
from transformers import
|
3 |
|
4 |
-
# Load GPT-2
|
5 |
-
|
|
|
|
|
6 |
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
-
# Create
|
14 |
-
|
15 |
-
fn=
|
16 |
-
inputs=gr.Chatbot(),
|
17 |
-
outputs=gr.Chatbot(),
|
18 |
title="Chat with GPT-2",
|
19 |
-
description="A simple
|
20 |
)
|
21 |
|
22 |
-
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import GPT2LMHeadModel, GPT2Tokenizer
|
3 |
|
4 |
+
# Load pre-trained GPT-2 model and tokenizer
|
5 |
+
model_name = "gpt2"
|
6 |
+
tokenizer = GPT2Tokenizer.from_pretrained(model_name)
|
7 |
+
model = GPT2LMHeadModel.from_pretrained(model_name)
|
8 |
|
9 |
+
def generate_response(message, history):
|
10 |
+
# Combine the conversation history with the new message
|
11 |
+
input_text = f"{message}"
|
12 |
+
|
13 |
+
# Tokenize input text
|
14 |
+
inputs = tokenizer.encode(input_text, return_tensors="pt")
|
15 |
+
|
16 |
+
# Generate response using GPT-2
|
17 |
+
outputs = model.generate(inputs, max_length=50, num_return_sequences=1)
|
18 |
+
|
19 |
+
# Decode generated text
|
20 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
21 |
+
|
22 |
+
return response
|
23 |
|
24 |
+
# Create ChatInterface
|
25 |
+
demo = gr.ChatInterface(
|
26 |
+
fn=generate_response,
|
|
|
|
|
27 |
title="Chat with GPT-2",
|
28 |
+
description="A simple chatbot powered by GPT-2."
|
29 |
)
|
30 |
|
31 |
+
# Launch the app
|
32 |
+
demo.launch()
|