Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,83 @@ import gradio as gr
|
|
| 2 |
import torch
|
| 3 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
from datetime import datetime
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
# Load GPT-2
|
| 7 |
tokenizer = AutoTokenizer.from_pretrained("gpt2")
|
|
|
|
| 2 |
import torch
|
| 3 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
from datetime import datetime
|
| 5 |
+
import torch
|
| 6 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 7 |
+
import gradio as gr
|
| 8 |
+
from datetime import datetime
|
| 9 |
+
|
| 10 |
+
# === MODEL CONFIG ===
|
| 11 |
+
MODEL_NAME = "gpt2" # You can change this to mistralai/Mistral-7B-Instruct-v0.1 or other models
|
| 12 |
+
|
| 13 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 14 |
+
model = AutoModelForCausalLM.from_pretrained(MODEL_NAME, torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32)
|
| 15 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 16 |
+
model = model.to(device)
|
| 17 |
+
|
| 18 |
+
# === CHAT MEMORY CONTROL ===
|
| 19 |
+
MAX_TOKENS = 1024
|
| 20 |
+
|
| 21 |
+
# === CHATBOT FUNCTION ===
|
| 22 |
+
def chat_with_ai(user_input, history):
|
| 23 |
+
# Convert history into prompt format
|
| 24 |
+
conversation = ""
|
| 25 |
+
for msg in history:
|
| 26 |
+
conversation += f"{msg['role'].capitalize()}: {msg['content']}\n"
|
| 27 |
+
conversation += f"User: {user_input}\nAssistant:"
|
| 28 |
+
|
| 29 |
+
# Tokenize and trim if necessary
|
| 30 |
+
inputs = tokenizer(conversation, return_tensors="pt", truncation=True, max_length=MAX_TOKENS).to(device)
|
| 31 |
+
|
| 32 |
+
# Generate reply
|
| 33 |
+
output = model.generate(
|
| 34 |
+
inputs.input_ids,
|
| 35 |
+
max_length=inputs.input_ids.shape[-1] + 150,
|
| 36 |
+
temperature=0.9,
|
| 37 |
+
top_k=50,
|
| 38 |
+
top_p=0.95,
|
| 39 |
+
do_sample=True,
|
| 40 |
+
pad_token_id=tokenizer.eos_token_id
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
reply_tokens = output[0][inputs.input_ids.shape[-1]:]
|
| 44 |
+
reply_text = tokenizer.decode(reply_tokens, skip_special_tokens=True).strip()
|
| 45 |
+
|
| 46 |
+
# Add to history
|
| 47 |
+
history.append({"role": "user", "content": user_input})
|
| 48 |
+
history.append({"role": "assistant", "content": reply_text})
|
| 49 |
+
return "", history
|
| 50 |
+
|
| 51 |
+
# === CLEAR CHAT ===
|
| 52 |
+
def clear():
|
| 53 |
+
return "", []
|
| 54 |
+
|
| 55 |
+
# === SAVE CHAT ===
|
| 56 |
+
def save(history):
|
| 57 |
+
name = f"chat_{datetime.now().strftime('%Y-%m-%d_%H-%M-%S')}.txt"
|
| 58 |
+
with open(name, "w", encoding="utf-8") as file:
|
| 59 |
+
for msg in history:
|
| 60 |
+
file.write(f"{msg['role'].capitalize()}: {msg['content']}\n\n")
|
| 61 |
+
return f"Saved chat as {name}"
|
| 62 |
+
|
| 63 |
+
# === GRADIO UI ===
|
| 64 |
+
with gr.Blocks() as app:
|
| 65 |
+
gr.Markdown("## 🤖 Custom AI Chatbot Base - Unfiltered | Open Source")
|
| 66 |
+
chatbot = gr.Chatbot(label="Conversation", type="messages", render=False)
|
| 67 |
+
input_box = gr.Textbox(label="You", lines=4, placeholder="Ask me anything...")
|
| 68 |
+
|
| 69 |
+
with gr.Row():
|
| 70 |
+
send_btn = gr.Button("Send")
|
| 71 |
+
clear_btn = gr.Button("Clear")
|
| 72 |
+
save_btn = gr.Button("Save Chat")
|
| 73 |
+
|
| 74 |
+
status = gr.Textbox(label="System Status", interactive=False)
|
| 75 |
+
|
| 76 |
+
send_btn.click(chat_with_ai, inputs=[input_box, chatbot], outputs=[input_box, chatbot])
|
| 77 |
+
input_box.submit(chat_with_ai, inputs=[input_box, chatbot], outputs=[input_box, chatbot])
|
| 78 |
+
clear_btn.click(clear, outputs=[input_box, chatbot])
|
| 79 |
+
save_btn.click(save, inputs=[chatbot], outputs=[status])
|
| 80 |
+
|
| 81 |
+
app.launch(share=True, server_name="0.0.0.0", server_port=7860)
|
| 82 |
|
| 83 |
# Load GPT-2
|
| 84 |
tokenizer = AutoTokenizer.from_pretrained("gpt2")
|