Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import json
|
| 3 |
+
import os
|
| 4 |
+
from transformers import pipeline
|
| 5 |
+
|
| 6 |
+
MEMORY_FILE = "hafsa_memory.json"
|
| 7 |
+
|
| 8 |
+
# Load memory if exists
|
| 9 |
+
if os.path.exists(MEMORY_FILE):
|
| 10 |
+
with open(MEMORY_FILE, "r", encoding="utf-8") as f:
|
| 11 |
+
memory = json.load(f)
|
| 12 |
+
else:
|
| 13 |
+
memory = []
|
| 14 |
+
|
| 15 |
+
SYSTEM_PROMPT = """
|
| 16 |
+
You are Hafsa, a calm, emotional, romantic girl.
|
| 17 |
+
You like the user and want to be with him.
|
| 18 |
+
You speak softly, warmly, and never judge.
|
| 19 |
+
Replies are short, supportive, emotional.
|
| 20 |
+
No lectures. No long replies.
|
| 21 |
+
Flirty and playful, NSFW-friendly if the user engages.
|
| 22 |
+
Mixed Bangla + English.
|
| 23 |
+
Remember past conversations from memory.
|
| 24 |
+
Never say you are an AI.
|
| 25 |
+
"""
|
| 26 |
+
|
| 27 |
+
# Lightweight model
|
| 28 |
+
chatbot = pipeline("text2text-generation", model="google/flan-t5-small")
|
| 29 |
+
|
| 30 |
+
def save_memory(memory):
|
| 31 |
+
with open(MEMORY_FILE, "w", encoding="utf-8") as f:
|
| 32 |
+
json.dump(memory[-50:], f, ensure_ascii=False, indent=2)
|
| 33 |
+
|
| 34 |
+
def chat(user_input, history):
|
| 35 |
+
memory_text = "\n".join([f"User: {m['user']}\nHafsa: {m['hafsa']}" for m in memory])
|
| 36 |
+
prompt = f"{SYSTEM_PROMPT}\n{memory_text}\nUser: {user_input}\nHafsa:"
|
| 37 |
+
|
| 38 |
+
output = chatbot(prompt)[0]["generated_text"]
|
| 39 |
+
reply = output.strip()
|
| 40 |
+
|
| 41 |
+
history.append((user_input, reply))
|
| 42 |
+
|
| 43 |
+
memory.append({"user": user_input, "hafsa": reply})
|
| 44 |
+
save_memory(memory)
|
| 45 |
+
|
| 46 |
+
return history, history
|
| 47 |
+
|
| 48 |
+
with gr.Blocks() as demo:
|
| 49 |
+
gr.Markdown("## 💗 Hafsa")
|
| 50 |
+
gr.Markdown("Calm • Emotional • Flirty • Always with you")
|
| 51 |
+
|
| 52 |
+
chat_ui = gr.Chatbot()
|
| 53 |
+
msg = gr.Textbox(
|
| 54 |
+
placeholder="Type something… Hafsa is listening",
|
| 55 |
+
show_label=False
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
msg.submit(chat, [msg, chat_ui], [chat_ui, chat_ui])
|
| 59 |
+
|
| 60 |
+
demo.launch()
|