AUXteam commited on
Commit
95826ae
·
verified ·
1 Parent(s): 0f5675f

Upload folder using huggingface_hub

Browse files
Files changed (2) hide show
  1. app.py +61 -1
  2. tinytroupe/utils/semantics.py +21 -0
app.py CHANGED
@@ -2,8 +2,9 @@ import sys
2
  import os
3
  import gradio as gr
4
  import json
 
5
  from tinytroupe.factory import TinyPersonFactory
6
- from tinytroupe.utils.semantics import select_best_persona
7
  from tinytroupe.simulation_manager import SimulationManager, SimulationConfig
8
  from tinytroupe.agent.social_types import Content
9
  from huggingface_hub import hf_hub_download, upload_file
@@ -125,6 +126,59 @@ def find_best_persona(criteria):
125
  return {"error": f"Error during persona matching: {str(e)}"}
126
 
127
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
128
  def generate_social_network_api(name, persona_count, network_type, focus_group_name=None):
129
  """
130
  Gradio API endpoint for generating a social network.
@@ -374,6 +428,12 @@ with gr.Blocks() as demo:
374
  api_name="find_best_persona"
375
  )
376
 
 
 
 
 
 
 
377
  # Invisible components to expose API endpoints
378
  # These won't be seen by regular UI users but will be available via /api
379
  with gr.Tab("Social Network API", visible=False):
 
2
  import os
3
  import gradio as gr
4
  import json
5
+ import glob
6
  from tinytroupe.factory import TinyPersonFactory
7
+ from tinytroupe.utils.semantics import select_best_persona, select_relevant_personas_utility
8
  from tinytroupe.simulation_manager import SimulationManager, SimulationConfig
9
  from tinytroupe.agent.social_types import Content
10
  from huggingface_hub import hf_hub_download, upload_file
 
126
  return {"error": f"Error during persona matching: {str(e)}"}
127
 
128
 
129
+ def load_example_personas():
130
+ """
131
+ Loads example personas from the tinytroupe library.
132
+ """
133
+ example_personas = []
134
+ # Path to the agents folder in tinytroupe/examples
135
+ agents_path = os.path.join("tinytroupe", "examples", "agents", "*.agent.json")
136
+ for file_path in glob.glob(agents_path):
137
+ try:
138
+ with open(file_path, 'r', encoding='utf-8') as f:
139
+ data = json.load(f)
140
+ if "persona" in data:
141
+ example_personas.append(data["persona"])
142
+ except Exception as e:
143
+ print(f"Error loading example persona from {file_path}: {e}")
144
+ return example_personas
145
+
146
+
147
+ def identify_personas(context):
148
+ """
149
+ Identifies appropriate personas from the Tresor and example agents based on context.
150
+ """
151
+ try:
152
+ # 1. Load Tresor personas (persisted JSON)
153
+ tresor_personas = load_persona_base()
154
+
155
+ # 2. Load Example personas from tinytroupe library
156
+ example_personas = load_example_personas()
157
+
158
+ all_available = tresor_personas + example_personas
159
+
160
+ if not all_available:
161
+ return {"error": "No personas available in Tresor or examples."}
162
+
163
+ # 3. Use LLM to filter/select which ones match the 'context'
164
+ # Returns a list of indices
165
+ indices = select_relevant_personas_utility(context, all_available)
166
+
167
+ selected = []
168
+ if isinstance(indices, list):
169
+ for i in indices:
170
+ try:
171
+ idx = int(i)
172
+ if 0 <= idx < len(all_available):
173
+ selected.append(all_available[idx])
174
+ except (ValueError, TypeError):
175
+ continue
176
+
177
+ return selected
178
+ except Exception as e:
179
+ return {"error": str(e)}
180
+
181
+
182
  def generate_social_network_api(name, persona_count, network_type, focus_group_name=None):
183
  """
184
  Gradio API endpoint for generating a social network.
 
428
  api_name="find_best_persona"
429
  )
430
 
431
+ with gr.Tab("Identify Personas API", visible=False):
432
+ api_id_context = gr.Textbox(label="Context")
433
+ api_id_btn = gr.Button("Identify Personas")
434
+ api_id_out = gr.JSON()
435
+ api_id_btn.click(identify_personas, inputs=[api_id_context], outputs=api_id_out, api_name="identify_personas")
436
+
437
  # Invisible components to expose API endpoints
438
  # These won't be seen by regular UI users but will be available via /api
439
  with gr.Tab("Social Network API", visible=False):
tinytroupe/utils/semantics.py CHANGED
@@ -286,3 +286,24 @@ def select_best_persona(criteria: str, personas: list) -> int:
286
  int: The index of the best matching persona, or -1 if none match.
287
  """
288
  # llm decorator will handle the body of this function
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
286
  int: The index of the best matching persona, or -1 if none match.
287
  """
288
  # llm decorator will handle the body of this function
289
+
290
+ @llm()
291
+ def select_relevant_personas_utility(context: str, personas: list) -> list:
292
+ """
293
+ Given a context and a list of personas (each a dictionary),
294
+ select which personas are relevant to the context.
295
+
296
+ Rules:
297
+ - Analyze each persona against the provided context.
298
+ - Return a LIST of indices (starting from 0) of the relevant personas.
299
+ - Return an empty list [] if none match.
300
+ - Provide the result as a JSON array of integers.
301
+
302
+ Args:
303
+ context (str): The context or requirements for persona selection.
304
+ personas (list): A list of dictionaries, where each dictionary is a persona specification.
305
+
306
+ Returns:
307
+ list: A list of indices of the matching personas.
308
+ """
309
+ # llm decorator will handle the body of this function