Spaces:
Runtime error
Runtime error
try5 app.py
Browse files
app.py
CHANGED
|
@@ -7,7 +7,7 @@ memory_db = {}
|
|
| 7 |
# Load the model
|
| 8 |
interpreter = pipeline("text2text-generation", model="google/flan-t5-base")
|
| 9 |
|
| 10 |
-
# Few-shot examples
|
| 11 |
few_shot_examples = """
|
| 12 |
Example 1:
|
| 13 |
"sure. whatever you want"
|
|
@@ -22,7 +22,7 @@ Example 3:
|
|
| 22 |
→ Likely masking frustration or disappointment, doesn’t want to express upset directly.
|
| 23 |
"""
|
| 24 |
|
| 25 |
-
# Interpret message and return
|
| 26 |
def interpret_message(msg, name, closeness, traits, relation, past_events, feelings, use_memory):
|
| 27 |
if not msg.strip() or not name.strip():
|
| 28 |
return "Please enter both a message and a person’s name.", ""
|
|
@@ -68,4 +68,83 @@ Infer their hidden intention, emotional state, and subtext. Then suggest how I s
|
|
| 68 |
"interpretation": "TO BE FILLED"
|
| 69 |
})
|
| 70 |
|
| 71 |
-
result = interpreter(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
# Load the model
|
| 8 |
interpreter = pipeline("text2text-generation", model="google/flan-t5-base")
|
| 9 |
|
| 10 |
+
# Few-shot examples to guide the model
|
| 11 |
few_shot_examples = """
|
| 12 |
Example 1:
|
| 13 |
"sure. whatever you want"
|
|
|
|
| 22 |
→ Likely masking frustration or disappointment, doesn’t want to express upset directly.
|
| 23 |
"""
|
| 24 |
|
| 25 |
+
# Interpret message and return explanation + memory log
|
| 26 |
def interpret_message(msg, name, closeness, traits, relation, past_events, feelings, use_memory):
|
| 27 |
if not msg.strip() or not name.strip():
|
| 28 |
return "Please enter both a message and a person’s name.", ""
|
|
|
|
| 68 |
"interpretation": "TO BE FILLED"
|
| 69 |
})
|
| 70 |
|
| 71 |
+
result = interpreter(prompt, max_new_tokens=80)[0]["generated_text"]
|
| 72 |
+
|
| 73 |
+
if use_memory:
|
| 74 |
+
memory_db[name]["history"][-1]["interpretation"] = result
|
| 75 |
+
|
| 76 |
+
memory_log = ""
|
| 77 |
+
if use_memory and memory_db[name]["history"]:
|
| 78 |
+
memory_log = "\n\n".join(
|
| 79 |
+
[f"• \"{entry['message']}\"\n → {entry['interpretation']}" for entry in memory_db[name]["history"]]
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
+
return result, memory_log
|
| 83 |
+
|
| 84 |
+
# Memory Advisor chatbot
|
| 85 |
+
def memory_chat(user_input, person_name):
|
| 86 |
+
name = person_name.strip().title()
|
| 87 |
+
if name not in memory_db:
|
| 88 |
+
return "There's no memory for this person yet. Try interpreting a message first."
|
| 89 |
+
|
| 90 |
+
history = memory_db[name]["history"]
|
| 91 |
+
past_text = "\n\n".join([f"- {h['message']}\n → {h['interpretation']}" for h in history])
|
| 92 |
+
|
| 93 |
+
chat_prompt = f"""You are a relationship and social communication expert trained in human behavior.
|
| 94 |
+
|
| 95 |
+
Here is what you know about {name}'s recent messages and past behavior:
|
| 96 |
+
{past_text}
|
| 97 |
+
|
| 98 |
+
The user now asks: {user_input}
|
| 99 |
+
|
| 100 |
+
Respond with a supportive and emotionally intelligent answer based on past messages.
|
| 101 |
+
Avoid repeating the user's input. Be specific. If {name} is likely upset, say it.
|
| 102 |
+
If the user should give space or check in, say so.
|
| 103 |
+
|
| 104 |
+
Only output your advice in 2–4 sentences. No explanation of what you’re doing.
|
| 105 |
+
"""
|
| 106 |
+
|
| 107 |
+
response = interpreter(chat_prompt, max_new_tokens=80)[0]["generated_text"]
|
| 108 |
+
return response
|
| 109 |
+
|
| 110 |
+
# Gradio UI
|
| 111 |
+
with gr.Blocks(title="Textual Chemistry") as app:
|
| 112 |
+
gr.Markdown("## 💬 Textual Chemistry\n_What do they really mean?_")
|
| 113 |
+
|
| 114 |
+
with gr.Row():
|
| 115 |
+
person_name = gr.Textbox(label="Person’s Name", placeholder="e.g., Alex")
|
| 116 |
+
|
| 117 |
+
message = gr.Textbox(label="Message Received", placeholder="Paste the message here...", lines=3)
|
| 118 |
+
|
| 119 |
+
with gr.Row():
|
| 120 |
+
closeness = gr.Dropdown(
|
| 121 |
+
choices=["Stranger", "Acquaintance", "Friend", "Close Friend", "Partner", "Ex", "Boss", "Teacher"],
|
| 122 |
+
label="Closeness Level")
|
| 123 |
+
use_memory = gr.Checkbox(label="Use memory for this person?", value=True)
|
| 124 |
+
|
| 125 |
+
sender_traits = gr.Textbox(label="Background on the Sender", placeholder="e.g., Sarcastic, hates small talk...")
|
| 126 |
+
relationship = gr.Textbox(label="Relationship History", placeholder="e.g., We used to date, now friends")
|
| 127 |
+
past_events = gr.Textbox(label="Similar Past Situations", placeholder="e.g., Last time I canceled they acted cold")
|
| 128 |
+
user_feelings = gr.Textbox(label="Your Current Feelings", placeholder="e.g., I feel confused or anxious")
|
| 129 |
+
|
| 130 |
+
output = gr.Textbox(label="🧠 Interpretation", lines=6)
|
| 131 |
+
history_output = gr.Textbox(label="🧠 Memory Log for This Person", lines=10)
|
| 132 |
+
|
| 133 |
+
submit_btn = gr.Button("Interpret Message")
|
| 134 |
+
submit_btn.click(
|
| 135 |
+
fn=interpret_message,
|
| 136 |
+
inputs=[message, person_name, closeness, sender_traits, relationship, past_events, user_feelings, use_memory],
|
| 137 |
+
outputs=[output, history_output]
|
| 138 |
+
)
|
| 139 |
+
|
| 140 |
+
gr.Markdown("---")
|
| 141 |
+
gr.Markdown("## 🧠 Memory Advisor Chatbot")
|
| 142 |
+
|
| 143 |
+
with gr.Row():
|
| 144 |
+
chatbot_input = gr.Textbox(label="Ask a follow-up question about this person")
|
| 145 |
+
chatbot_output = gr.Textbox(label="🤖 Advisor Response", lines=6)
|
| 146 |
+
|
| 147 |
+
chatbot_btn = gr.Button("Ask Memory Bot")
|
| 148 |
+
chatbot_btn.click(fn=memory_chat, inputs=[chatbot_input, person_name], outputs=chatbot_output)
|
| 149 |
+
|
| 150 |
+
app.launch()
|