File size: 1,189 Bytes
54e8c3e
fbfc910
6672892
54e8c3e
6672892
 
fbfc910
 
 
6672892
 
 
 
fbfc910
6672892
 
 
 
 
 
 
 
fbfc910
6672892
 
 
 
 
 
fbfc910
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import gradio as gr
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch

# Load the pre-trained DistilGPT-2 model and tokenizer
model_name = "distilgpt2"  # This is a smaller version of GPT-2
model = AutoModelForCausalLM.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name)

# Function to generate text based on user input
def generate_response(user_input):
    # Encode the input prompt
    inputs = tokenizer.encode(user_input, return_tensors="pt")

    # Generate a response from the model
    with torch.no_grad():  # Disable gradient calculation for inference
        outputs = model.generate(inputs, max_length=100, num_return_sequences=1, no_repeat_ngram_size=2, top_p=0.9, top_k=50)
    
    # Decode the generated response
    response = tokenizer.decode(outputs[0], skip_special_tokens=True)
    
    return response

# Create Gradio interface for text input and output
iface = gr.Interface(fn=generate_response, inputs="text", outputs="text", 
                     title="DistilGPT-2 Chatbot", 
                     description="A lightweight conversational chatbot using DistilGPT-2.")

# Launch the Gradio app
iface.launch()