File size: 4,596 Bytes
534f131
 
3e49468
534f131
 
4d040b2
534f131
 
3e49468
534f131
 
 
 
 
 
 
706eba1
534f131
 
 
 
 
706eba1
534f131
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3e49468
534f131
3e49468
534f131
 
 
3ad0ace
534f131
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3ad0ace
a2583e0
3ad0ace
 
 
 
 
 
 
 
 
 
534f131
 
 
 
 
706eba1
534f131
 
706eba1
767a8fb
534f131
 
706eba1
534f131
 
 
 
 
 
 
 
 
706eba1
534f131
3e49468
706eba1
534f131
a2583e0
3e49468
534f131
a2583e0
 
706eba1
 
 
 
 
 
 
 
 
 
a2583e0
 
 
 
706eba1
 
 
 
534f131
 
 
 
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import torch
import torch.nn as nn
import random
from transformers import GPT2LMHeadModel, GPT2Tokenizer
from textblob import TextBlob
import gradio as gr
import pickle
import numpy as np
import torch.nn.functional as F

# ---- Constants and Setup ----
model_name = 'gpt2'
tokenizer = GPT2Tokenizer.from_pretrained(model_name)
model = GPT2LMHeadModel.from_pretrained(model_name)
model.eval()

# Ensure tokenizer pad token is set
if tokenizer.pad_token is None:
    tokenizer.pad_token = tokenizer.eos_token

tokenizer.clean_up_tokenization_spaces = True

# Set device for model and tensors
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model.to(device)

# ---- Memory Management ----
session_memory = []

def save_memory(memory, filename='chat_memory.pkl'):
    with open(filename, 'wb') as f:
        pickle.dump(memory, f)

def load_memory(filename='chat_memory.pkl'):
    try:
        with open(filename, 'rb') as f:
            return pickle.load(f)
    except FileNotFoundError:
        return []

session_memory = load_memory()

# ---- Sentiment Analysis ----
def analyze_sentiment(text):
    blob = TextBlob(text)
    return blob.sentiment.polarity  # Range from -1 (negative) to 1 (positive)

def adjust_for_emotion(response, sentiment):
    if sentiment > 0.2:
        return f"That's wonderful! I'm glad you're feeling good: {response}"
    elif sentiment < -0.2:
        return f"I'm sorry to hear that: {response}. How can I assist you further?"
    return response

# ---- Response Generation ----
def generate_response(prompt, max_length=512):
    inputs = tokenizer(prompt, return_tensors='pt', padding=True, truncation=True, max_length=max_length)
    input_ids = inputs['input_ids'].to(device)
    attention_mask = inputs['attention_mask'].to(device)
    pad_token_id = tokenizer.pad_token_id

    with torch.no_grad():
        output = model.generate(
            input_ids,
            attention_mask=attention_mask,
            max_length=max_length,
            num_return_sequences=1,
            no_repeat_ngram_size=2,
            do_sample=True,
            temperature=0.9,
            top_k=50,
            top_p=0.95,
            early_stopping=False,
            pad_token_id=pad_token_id
        )

    response = tokenizer.decode(output[0], skip_special_tokens=True)

    # Split response into two parts and apply color
    parts = response.split("\n", 1)
    if len(parts) > 1:
        before_indent = f'<span style="color: orange;">{parts[0].strip()}</span>'
        after_indent = f'<span style="color: blue;">Inner Thoughts: {parts[1].strip()}</span>'
        colored_response = before_indent + '\n' + after_indent
    else:
        colored_response = f'<span style="color: orange;">{response.strip()}</span>'

    return colored_response

# ---- Interactive Chat Function ----
def advanced_agi_chat(user_input):
    session_memory.append({"input": user_input})
    save_memory(session_memory)

    # Sentiment analysis of user input
    user_sentiment = analyze_sentiment(user_input)

    # Generate the response based on the prompt
    prompt = f"User: {user_input}\nAutistic-Gertrude:"
    response = generate_response(prompt)

    # Adjust the response based on sentiment
    adjusted_response = adjust_for_emotion(response, user_sentiment)

    return adjusted_response

# ---- Gradio Interface ----
def chat_interface(user_input):
    response = advanced_agi_chat(user_input)
    return response

# ---- Gradio App Setup ----
with gr.Blocks() as app:
    gr.Markdown("# **Autistic Assistant vß Edition 2024 Ultra: Gertrude's Autistic Experience**")
    
    with gr.Row():
        with gr.Column(scale=1):
            user_input = gr.Textbox(label="What will you say to Gertrude?", placeholder="Type something here... Expect 1-2 Minute Response Times...")
            submit_button = gr.Button("Send")
        with gr.Column(scale=1):
            chatbot = gr.Textbox(label="Gertrude's Response", interactive=False)  # This is now a Textbox for output

    # Adding custom styling for the UI
    gr.HTML("""
        <style>
            .gradio-container { 
                background-color: #F4F8FF; 
                padding: 20px; 
                border-radius: 15px; 
                font-family: 'Comic Sans MS'; 
            }
            .gradio-row { 
                display: flex;
                justify-content: space-between;
            }
        </style>
    """)

    # Setting the button click event
    submit_button.click(chat_interface, inputs=user_input, outputs=chatbot)

# Launch the Gradio app
app.launch()