YUVALON commited on
Commit
18ea3c5
Β·
verified Β·
1 Parent(s): 535b755

UpdateBen app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -26
app.py CHANGED
@@ -1,20 +1,20 @@
1
  import gradio as gr
2
  from transformers import pipeline
3
 
4
- # πŸ” In-memory memory store
5
  memory_db = {}
6
 
7
- # πŸ€– Load Hugging Face text-to-text model (CPU-friendly)
8
  interpreter = pipeline("text2text-generation", model="google/flan-t5-base")
9
 
10
- # 🧠 Core message interpretation function
11
  def interpret_message(msg, name, closeness, traits, relation, past_events, feelings, use_memory):
12
  if not msg.strip() or not name.strip():
13
- return "Please enter both a message and a person’s name."
14
 
15
  name = name.strip().title()
16
 
17
- # Load memory if exists
18
  if use_memory:
19
  if name in memory_db:
20
  existing = memory_db[name]
@@ -29,19 +29,10 @@ def interpret_message(msg, name, closeness, traits, relation, past_events, feeli
29
  "history": []
30
  }
31
 
32
- # Create structured prompt
33
- prompt = f"""New message from {name}: "{msg}"
34
 
35
- Known context:
36
- - Closeness: {closeness}
37
- - Personality: {traits}
38
- - Relationship: {relation}
39
- - Similar Past Events: {past_events}
40
- - Your Feelings: {feelings}
41
-
42
- Interpret the likely tone and intention. Suggest a possible meaning and how I could respond."""
43
-
44
- # Update memory before model (to include prompt)
45
  if use_memory:
46
  memory_db[name]["closeness"] = closeness
47
  memory_db[name]["personality"] = traits
@@ -52,16 +43,38 @@ Interpret the likely tone and intention. Suggest a possible meaning and how I co
52
  "interpretation": "TO BE FILLED"
53
  })
54
 
55
- # Generate interpretation from model
56
- result = interpreter(prompt, max_new_tokens=150)[0]["generated_text"]
57
 
58
- # Update memory with interpretation
59
  if use_memory:
60
  memory_db[name]["history"][-1]["interpretation"] = result
61
 
62
- return f"🧠 Memory active for {name}.\n\n**Interpretation:**\n{result}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
 
64
- # πŸŽ›οΈ Gradio UI
 
 
 
65
  with gr.Blocks(title="Textual Chemistry") as app:
66
  gr.Markdown("## πŸ’¬ Textual Chemistry\n_What do they really mean?_")
67
 
@@ -73,8 +86,7 @@ with gr.Blocks(title="Textual Chemistry") as app:
73
  with gr.Row():
74
  closeness = gr.Dropdown(
75
  choices=["Stranger", "Acquaintance", "Friend", "Close Friend", "Partner", "Ex", "Boss", "Teacher"],
76
- label="Closeness Level"
77
- )
78
  use_memory = gr.Checkbox(label="Use memory for this person?", value=True)
79
 
80
  sender_traits = gr.Textbox(label="Background on the Sender", placeholder="e.g., Sarcastic, hates small talk...")
@@ -82,13 +94,24 @@ with gr.Blocks(title="Textual Chemistry") as app:
82
  past_events = gr.Textbox(label="Similar Past Situations", placeholder="e.g., Last time I canceled they acted cold")
83
  user_feelings = gr.Textbox(label="Your Current Feelings", placeholder="e.g., I feel confused or anxious")
84
 
85
- output = gr.Textbox(label="AI Interpretation", lines=10)
 
86
 
87
  submit_btn = gr.Button("Interpret Message")
88
  submit_btn.click(
89
  fn=interpret_message,
90
  inputs=[message, person_name, closeness, sender_traits, relationship, past_events, user_feelings, use_memory],
91
- outputs=output
92
  )
93
 
 
 
 
 
 
 
 
 
 
 
94
  app.launch()
 
1
  import gradio as gr
2
  from transformers import pipeline
3
 
4
+ # In-memory memory store
5
  memory_db = {}
6
 
7
+ # Load the model
8
  interpreter = pipeline("text2text-generation", model="google/flan-t5-base")
9
 
10
+ # Interpret a message with optional memory and return AI explanation + history
11
  def interpret_message(msg, name, closeness, traits, relation, past_events, feelings, use_memory):
12
  if not msg.strip() or not name.strip():
13
+ return "Please enter both a message and a person’s name.", ""
14
 
15
  name = name.strip().title()
16
 
17
+ # Initialize or update memory
18
  if use_memory:
19
  if name in memory_db:
20
  existing = memory_db[name]
 
29
  "history": []
30
  }
31
 
32
+ # Construct rich prompt
33
+ prompt = f"""New message from {name}: \"{msg}\"\n\nKnown context:\n- Closeness: {closeness}\n- Personality: {traits}\n- Relationship: {relation}\n- Similar Past Events: {past_events}\n- Your Feelings: {feelings}\n\nBased on the message and context, explain what the person is *really thinking* or feeling but not saying directly.\nInfer their hidden intention, emotional state, and subtext. Then suggest how I should respond."""
34
 
35
+ # Save to memory if applicable
 
 
 
 
 
 
 
 
 
36
  if use_memory:
37
  memory_db[name]["closeness"] = closeness
38
  memory_db[name]["personality"] = traits
 
43
  "interpretation": "TO BE FILLED"
44
  })
45
 
46
+ # Run model
47
+ result = interpreter(prompt, max_new_tokens=200)[0]["generated_text"]
48
 
 
49
  if use_memory:
50
  memory_db[name]["history"][-1]["interpretation"] = result
51
 
52
+ # Format memory log
53
+ memory_log = ""
54
+ if use_memory and memory_db[name]["history"]:
55
+ memory_log = "\n\n".join(
56
+ [f"\u2022 \"{entry['message']}\"\n \u2192 {entry['interpretation']}" for entry in memory_db[name]["history"]]
57
+ )
58
+
59
+ return result, memory_log
60
+
61
+ # Chatbot advisor based on memory
62
+ def memory_chat(user_input, person_name):
63
+ name = person_name.strip().title()
64
+ if name not in memory_db:
65
+ return "There's no memory for this person yet. Try interpreting a message first."
66
+
67
+ # Summarize memory history into prompt
68
+ history = memory_db[name]["history"]
69
+ past_text = "\n\n".join([f"- {h['message']}\n -> {h['interpretation']}" for h in history])
70
+
71
+ chat_prompt = f"""You are a smart advisor trained on past conversations with {name}.\n
72
+ Here is the message history:\n{past_text}\n\nUser asks: {user_input}\n\nGive a thoughtful answer using patterns or emotional tone from the past history."""
73
 
74
+ response = interpreter(chat_prompt, max_new_tokens=200)[0]["generated_text"]
75
+ return response
76
+
77
+ # Gradio UI
78
  with gr.Blocks(title="Textual Chemistry") as app:
79
  gr.Markdown("## πŸ’¬ Textual Chemistry\n_What do they really mean?_")
80
 
 
86
  with gr.Row():
87
  closeness = gr.Dropdown(
88
  choices=["Stranger", "Acquaintance", "Friend", "Close Friend", "Partner", "Ex", "Boss", "Teacher"],
89
+ label="Closeness Level")
 
90
  use_memory = gr.Checkbox(label="Use memory for this person?", value=True)
91
 
92
  sender_traits = gr.Textbox(label="Background on the Sender", placeholder="e.g., Sarcastic, hates small talk...")
 
94
  past_events = gr.Textbox(label="Similar Past Situations", placeholder="e.g., Last time I canceled they acted cold")
95
  user_feelings = gr.Textbox(label="Your Current Feelings", placeholder="e.g., I feel confused or anxious")
96
 
97
+ output = gr.Textbox(label="🧠 Interpretation", lines=6)
98
+ history_output = gr.Textbox(label="🧠 Memory Log for This Person", lines=10)
99
 
100
  submit_btn = gr.Button("Interpret Message")
101
  submit_btn.click(
102
  fn=interpret_message,
103
  inputs=[message, person_name, closeness, sender_traits, relationship, past_events, user_feelings, use_memory],
104
+ outputs=[output, history_output]
105
  )
106
 
107
+ gr.Markdown("---")
108
+ gr.Markdown("## 🧠 Memory Advisor Chatbot")
109
+
110
+ with gr.Row():
111
+ chatbot_input = gr.Textbox(label="Ask a follow-up question about this person")
112
+ chatbot_output = gr.Textbox(label="πŸ€– Advisor Response", lines=6)
113
+
114
+ chatbot_btn = gr.Button("Ask Memory Bot")
115
+ chatbot_btn.click(fn=memory_chat, inputs=[chatbot_input, person_name], outputs=chatbot_output)
116
+
117
  app.launch()