Spaces:
No application file
No application file
Update app.py
Browse files
app.py
CHANGED
@@ -1,19 +1,30 @@
|
|
1 |
import gradio as gr
|
2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
|
|
3 |
|
4 |
-
# Load
|
5 |
-
model_name = "
|
6 |
model = AutoModelForCausalLM.from_pretrained(model_name)
|
7 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
8 |
|
9 |
-
# Function to generate text
|
10 |
-
def
|
11 |
-
|
12 |
-
|
13 |
-
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
14 |
|
15 |
-
#
|
16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
-
#
|
|
|
|
|
|
|
|
|
|
|
19 |
iface.launch()
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
+
import torch
|
4 |
|
5 |
+
# Load the pre-trained DistilGPT-2 model and tokenizer
|
6 |
+
model_name = "distilgpt2" # This is a smaller version of GPT-2
|
7 |
model = AutoModelForCausalLM.from_pretrained(model_name)
|
8 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
9 |
|
10 |
+
# Function to generate text based on user input
|
11 |
+
def generate_response(user_input):
|
12 |
+
# Encode the input prompt
|
13 |
+
inputs = tokenizer.encode(user_input, return_tensors="pt")
|
|
|
14 |
|
15 |
+
# Generate a response from the model
|
16 |
+
with torch.no_grad(): # Disable gradient calculation for inference
|
17 |
+
outputs = model.generate(inputs, max_length=100, num_return_sequences=1, no_repeat_ngram_size=2, top_p=0.9, top_k=50)
|
18 |
+
|
19 |
+
# Decode the generated response
|
20 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
21 |
+
|
22 |
+
return response
|
23 |
|
24 |
+
# Create Gradio interface for text input and output
|
25 |
+
iface = gr.Interface(fn=generate_response, inputs="text", outputs="text",
|
26 |
+
title="DistilGPT-2 Chatbot",
|
27 |
+
description="A lightweight conversational chatbot using DistilGPT-2.")
|
28 |
+
|
29 |
+
# Launch the Gradio app
|
30 |
iface.launch()
|