Update app.py
Browse files
app.py
CHANGED
@@ -1,17 +1,32 @@
|
|
1 |
-
|
|
|
2 |
|
3 |
-
#
|
4 |
-
|
|
|
|
|
5 |
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
top_k=
|
13 |
-
|
14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
)
|
16 |
|
17 |
-
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import GPT2LMHeadModel, GPT2Tokenizer
|
3 |
|
4 |
+
# Load the GPT-2 model and tokenizer
|
5 |
+
model_name = "gpt2" # You can also try "gpt2-medium" or "gpt2-large" for more powerful models
|
6 |
+
model = GPT2LMHeadModel.from_pretrained(model_name)
|
7 |
+
tokenizer = GPT2Tokenizer.from_pretrained(model_name)
|
8 |
|
9 |
+
# Define the chat function
|
10 |
+
def chat_function(user_input):
|
11 |
+
# Encode the input
|
12 |
+
inputs = tokenizer.encode(user_input + tokenizer.eos_token, return_tensors="pt")
|
13 |
+
|
14 |
+
# Generate a response from the model
|
15 |
+
outputs = model.generate(inputs, max_length=150, num_return_sequences=1, no_repeat_ngram_size=2, top_k=50, top_p=0.95, temperature=0.7)
|
16 |
+
|
17 |
+
# Decode the generated response
|
18 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
19 |
+
|
20 |
+
return response
|
21 |
+
|
22 |
+
# Create the Gradio interface
|
23 |
+
iface = gr.Interface(
|
24 |
+
fn=chat_function, # Function to call
|
25 |
+
inputs=gr.Textbox(label="You"), # Textbox for user input
|
26 |
+
outputs=gr.Textbox(label="Chatbot Response"), # Textbox for model output
|
27 |
+
title="GPT-2 Chatbot", # Title of the app
|
28 |
+
description="A simple chatbot powered by GPT-2. Enter a message to chat with the bot." # Description
|
29 |
)
|
30 |
|
31 |
+
# Launch the app
|
32 |
+
iface.launch()
|