File size: 4,319 Bytes
2625e36
 
 
 
 
 
 
 
 
 
 
111bd60
2625e36
 
 
 
111bd60
2625e36
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
111bd60
 
 
2625e36
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
111bd60
2625e36
 
111bd60
 
 
 
 
 
 
 
 
2625e36
 
111bd60
 
 
 
 
 
 
 
 
 
2625e36
 
 
111bd60
2625e36
 
 
 
 
111bd60
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# -*- coding: utf-8 -*-
"""nino bot

Automatically generated by Colab.

Original file is located at
    https://colab.research.google.com/drive/1UgXple_p_R-0mq9p5vhOmFPo9cgdayJy
"""
import gradio as gr
from transformers import AutoModelForCausalLM, AutoTokenizer

# Load the pretrained DialoGPT model
model_name = "microsoft/DialoGPT-small"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)

# Hardcoded rule-based responses
rules = {
    "hi": "Hello! How can I help you today?",
    "hello": "Hi there! How can I assist you?",
    "hey": "Hey! What can I do for you?",
    "how are you": "I'm just a bot, but I'm doing great! 😊 How about you?",
    "good morning": "Good morning! Hope you have a wonderful day!",
    "good afternoon": "Good afternoon! How can I help you?",
    "good evening": "Good evening! What can I do for you?",
    "bye": "Goodbye! Have a nice day! πŸ‘‹",
    "thank you": "You're welcome! 😊",
    "thanks": "No problem! Happy to help!",
    "what is your name": "I'm your friendly chatbot assistant.",
    "help": "Sure! Ask me anything or type 'bye' to exit.",
    "what can you do": "I can answer simple questions and chat with you. Try saying hi!",
    "tell me a joke": "Why did the computer show up at work late? It had a hard drive!",
    "what time is it": "Sorry, I don't have a clock yet. But you can check your device's time!",
    "where are you from": "I'm from the cloud, here to assist you anytime!",
    "what is ai": "AI stands for Artificial Intelligence, which is intelligence demonstrated by machines.",
    "who created you": "I was created by a talented developer using Python and machine learning!",
    "how can i learn programming": "Start with basics like Python. There are many free tutorials online to get you started!",
    "ok": "ok",
    "who are you?": "I am nino",
    "hi nino": "hi there",
}

def respond(user_input, history):
    if not isinstance(history, list):
        history = []
    if not isinstance(user_input, str):
        user_input = str(user_input)

    user_input_clean = user_input.lower().strip()

    if user_input_clean in rules:
        bot_reply = rules[user_input_clean]
    else:
        prompt = ""
        for user_msg, bot_msg in history:
            if bot_msg is not None:
                prompt += f"{user_msg} {tokenizer.eos_token}\n{bot_msg} {tokenizer.eos_token}\n"
            else:
                prompt += f"{user_msg} {tokenizer.eos_token}\n"
        prompt += f"{user_input} {tokenizer.eos_token}\n"

        inputs = tokenizer(prompt, return_tensors="pt")
        outputs = model.generate(
            **inputs,
            max_new_tokens=100,
            pad_token_id=tokenizer.eos_token_id,
            do_sample=True,
            temperature=0.7,
            top_p=0.9,
        )

        generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
        bot_reply = generated_text[len(prompt):].strip()

        if len(bot_reply) < 5 or bot_reply.lower() in ["", "idk", "i don't know", "huh"]:
            bot_reply = "I'm not sure how to respond to that. Can you rephrase it?"

    history.append((user_input, bot_reply))
    return history, "", history

def save_chat(history):
    try:
        with open("/tmp/chat_history.txt", "w", encoding="utf-8") as f:
            for user_msg, bot_msg in history:
                if bot_msg is not None:
                    f.write(f"You: {user_msg}\nBot: {bot_msg}\n\n")
                else:
                    f.write(f"You: {user_msg}\nBot: (No response)\n\n")
    except Exception as e:
        print("Error saving chat:", e)

def process_input(user_input, history):
    try:
        result = respond(user_input, history)
        save_chat(result[0])
        return result
    except Exception as e:
        error_message = f"Error: {str(e)}"
        if not isinstance(history, list):
            history = []
        history.append((user_input, error_message))
        return history, "", history

with gr.Blocks() as demo:
    chatbot = gr.Chatbot()
    msg = gr.Textbox(placeholder="Type your message here...", show_label=False)
    state = gr.State([])

    msg.submit(process_input, inputs=[msg, state], outputs=[chatbot, msg, state])

if __name__ == "__main__":
    demo.launch()