File size: 1,092 Bytes
5d8396d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
32
33
34
35
36
37
38
import gradio as gr
from transformers import GPT2LMHeadModel, GPT2Tokenizer, pipeline

# Load model and tokenizer from Hugging Face
model_name = "taiy0/Rae"
model = GPT2LMHeadModel.from_pretrained(model_name)
tokenizer = GPT2Tokenizer.from_pretrained(model_name)

# Create chatbot pipeline
chatbot = pipeline("text-generation", model=model, tokenizer=tokenizer, truncation=True)

# Function to generate chatbot responses
def generate_response(user_input):
    prompt = f"<|user|> {user_input}\n<|bot|>"
    
    response = chatbot(
        prompt,
        max_length=100,
        do_sample=True,
        top_p=0.85,
        truncation=True
    )[0]["generated_text"]
    
    # Extract only the chatbot's response
    return response.split("<|bot|>")[-1].strip()

# Create Gradio UI
iface = gr.Interface(
    fn=generate_response,
    inputs=gr.Textbox(lines=2, placeholder="Type a message..."),
    outputs="text",
    title="chatbot-api",
    description="A chatbot fine-tuned by Aditya Naidu (Taiyo).",
)

# Launch the chatbot
iface.launch(share=True)