Spaces:
Runtime error
Runtime error
File size: 3,834 Bytes
d031645 f53d982 0451bcb f53d982 703dbf1 f3a62c3 3154d2b 0451bcb 31f5134 f3a62c3 0451bcb 31f5134 f3a62c3 703dbf1 3154d2b 0451bcb 3154d2b 0451bcb 3154d2b 0451bcb 3154d2b 0451bcb 3154d2b 0451bcb 3154d2b 0451bcb 3154d2b 703dbf1 3154d2b 703dbf1 0451bcb 3154d2b ec08846 0451bcb 703dbf1 0451bcb 703dbf1 3154d2b 703dbf1 f53d982 3154d2b 0451bcb f53d982 3154d2b f53d982 f3a62c3 0451bcb f3a62c3 0451bcb 3154d2b f53d982 f3a62c3 ec08846 f3a62c3 f53d982 f3a62c3 ec08846 f3a62c3 f53d982 0451bcb f3a62c3 f53d982 82f4fbd | 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 | import gradio as gr
import torch
import json
import os
import time
from transformers import AutoTokenizer, AutoModelForCausalLM
# === CONFIGURATION ===
MODEL_NAME = "facebook/opt-350m" # β
Fast and lightweight
MEMORY_FILE = "memory.json"
# === LOAD MODEL ===
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
model = AutoModelForCausalLM.from_pretrained(
MODEL_NAME,
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
device_map="auto"
)
# === MEMORY ===
def load_memory(file_path):
if os.path.exists(file_path):
with open(file_path, "r", encoding="utf-8") as f:
return json.load(f)
return []
def save_memory(file_path, data):
with open(file_path, "w", encoding="utf-8") as f:
json.dump(data, f, indent=4)
memory = load_memory(MEMORY_FILE)
# === CHAT FUNCTION ===
def chat_with_ai(user_input, history, turbo_mode):
memory.append({"user": user_input})
save_memory(MEMORY_FILE, memory)
conversation = ""
for msg in memory[-3:]: # Only the last 3 inputs to keep it lightweight
conversation += f"User: {msg['user']}\nAssistant:"
inputs = tokenizer(conversation, return_tensors="pt", truncation=True, max_length=512).to(model.device)
generate_args = {
'input_ids': inputs.input_ids,
'max_length': inputs.input_ids.shape[-1] + (30 if turbo_mode else 50),
'temperature': 0.6,
'top_k': 30,
'top_p': 0.9,
'do_sample': True,
'pad_token_id': tokenizer.eos_token_id,
'early_stopping': True
}
output = model.generate(**generate_args)
reply_tokens = output[0][inputs.input_ids.shape[-1]:]
reply_text = tokenizer.decode(reply_tokens, skip_special_tokens=True).strip()
# Save Assistant reply to memory
memory.append({"assistant": reply_text})
save_memory(MEMORY_FILE, memory)
return "", history + [{"role": "user", "content": user_input}, {"role": "assistant", "content": reply_text}], reply_text
def clear_chat():
memory.clear()
save_memory(MEMORY_FILE, [])
return "", [], ""
def save_chat(history):
timestamp = time.strftime("%Y-%m-%d_%H-%M-%S")
filename = f"chat_{timestamp}.txt"
with open(filename, "w", encoding="utf-8") as f:
for msg in history:
f.write(f"{msg['role'].capitalize()}: {msg['content']}\n\n")
return f"πΎ Chat saved as {filename}."
# === DARK THEME CSS ===
css = """
body {
background-color: #121212;
color: #e0e0e0;
}
.gradio-container, .gr-block, .gr-button, .gr-textbox {
background-color: #1e1e1e !important;
border-color: #333 !important;
}
.gr-button:hover {
background-color: #333333 !important;
}
"""
# === GRADIO APP ===
with gr.Blocks(css=css) as app:
gr.Markdown("## π€ Ultra Fast Living AI - Clean Mode", elem_id="title")
chatbot = gr.Chatbot(label="Chat History", type="messages", render=False)
user_input = gr.Textbox(lines=3, label="Your Message", placeholder="Type your message...")
ai_output = gr.Textbox(lines=5, label="π€ AI's Response", interactive=False, placeholder="AI will respond here...")
with gr.Row():
send_btn = gr.Button("Send")
clear_btn = gr.Button("Clear Chat")
save_btn = gr.Button("Save Chat")
turbo_mode = gr.Checkbox(label="π Turbo Mode (Faster Replies)", value=True)
status_box = gr.Textbox(label="System Status", interactive=False)
send_btn.click(chat_with_ai, inputs=[user_input, chatbot, turbo_mode], outputs=[user_input, chatbot, ai_output])
user_input.submit(chat_with_ai, inputs=[user_input, chatbot, turbo_mode], outputs=[user_input, chatbot, ai_output])
clear_btn.click(clear_chat, outputs=[user_input, chatbot, ai_output])
save_btn.click(save_chat, inputs=[chatbot], outputs=[status_box])
app.launch() |