AUXteam commited on
Commit
4b62e3e
·
verified ·
1 Parent(s): dca0bc9

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +105 -2
app.py CHANGED
@@ -42,6 +42,109 @@ simulation_manager = DummySimulationManager()
42
 
43
  REMOTE_BACKEND = "https://auxteam-tiny-factory.hf.space"
44
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
  def generate_personas(business_description, customer_profile, num_personas, api_key=None):
46
  if api_key:
47
  os.environ["OPENAI_API_KEY"] = api_key
@@ -306,8 +409,8 @@ def update_persona_preview(file):
306
 
307
  def generate_personas_router(business_description, customer_profile, num_personas, method, example_file, api_key=None):
308
  if method == "DeepPersona":
309
- # Call the existing DeepPersona implementation
310
- return generate_personas(business_description, customer_profile, num_personas, api_key)
311
  elif method == "TinyTroupe":
312
  if api_key:
313
  os.environ["OPENAI_API_KEY"] = api_key
 
42
 
43
  REMOTE_BACKEND = "https://auxteam-tiny-factory.hf.space"
44
 
45
+
46
+ # =========================================================================
47
+ # External/Hybrid Persona Generation Logic
48
+ # =========================================================================
49
+
50
+ CONFIG = {
51
+ "persona_generation": {
52
+ "mode": "hybrid", # internal, external, hybrid
53
+ "external_api_url": "THzva/deeppersona-experience",
54
+ "fallback_to_internal": True
55
+ },
56
+ "action_generation": {
57
+ "use_internal_llm": True,
58
+ "external_llm_endpoint": None
59
+ }
60
+ }
61
+
62
+ class InternalPersonaGenerator:
63
+ def generate(self, business_description, customer_profile, num_personas, api_key=None):
64
+ if api_key:
65
+ os.environ["OPENAI_API_KEY"] = api_key
66
+ get_tinytroupe_modules()
67
+ from tinytroupe.factory.tiny_person_factory import TinyPersonFactory
68
+ factory = TinyPersonFactory(context=f"{business_description} {customer_profile}")
69
+ personas = factory.generate_people(number_of_people=int(num_personas))
70
+ return [p._persona for p in personas]
71
+
72
+ class ExternalPersonaGenerator:
73
+ def generate(self, business_description, customer_profile, num_personas, api_key=None):
74
+ return generate_personas(business_description, customer_profile, num_personas, api_key)
75
+
76
+ class HybridPersonaGenerator:
77
+ def __init__(self, use_external=True):
78
+ self.use_external = use_external
79
+
80
+ def generate(self, business_description, customer_profile, num_personas, api_key=None):
81
+ # We can try external first, and if it fails or returns less, fallback
82
+ personas = ExternalPersonaGenerator().generate(business_description, customer_profile, num_personas, api_key)
83
+ if not personas and CONFIG["persona_generation"]["fallback_to_internal"]:
84
+ return InternalPersonaGenerator().generate(business_description, customer_profile, num_personas, api_key)
85
+ return personas
86
+
87
+ def create_persona(name: str, business_description: str, customer_profile: str, num_personas: int, api_key=None):
88
+ mode = CONFIG["persona_generation"]["mode"]
89
+ if mode == "external":
90
+ return ExternalPersonaGenerator().generate(business_description, customer_profile, num_personas, api_key)
91
+ elif mode == "hybrid":
92
+ try:
93
+ return HybridPersonaGenerator(use_external=True).generate(business_description, customer_profile, num_personas, api_key)
94
+ except Exception as e:
95
+ print("Hybrid generation exception:", e)
96
+ if CONFIG["persona_generation"]["fallback_to_internal"]:
97
+ return InternalPersonaGenerator().generate(business_description, customer_profile, num_personas, api_key)
98
+ raise
99
+ else:
100
+ return InternalPersonaGenerator().generate(business_description, customer_profile, num_personas, api_key)
101
+
102
+ def get_APIEnhancedTinyPerson():
103
+ get_tinytroupe_modules()
104
+ class APIEnhancedTinyPerson(TinyPerson):
105
+ """TinyPerson with external API integration"""
106
+ def __init__(self, name: str, use_api: bool = False, **api_config):
107
+ super().__init__(name)
108
+ self.use_api = use_api
109
+ from gradio_client import Client
110
+ self.api_client = Client(api_config.get('api_url', CONFIG["persona_generation"]["external_api_url"])) if use_api else None
111
+
112
+ def enhance_with_api(self, **params):
113
+ if not self.use_api:
114
+ raise RuntimeError("API mode not enabled")
115
+
116
+ result = self.api_client.predict(
117
+ age=float(params.get('age', self._get_attr('age', 25))),
118
+ gender=str(params.get('gender', self._get_attr('gender', 'Female'))),
119
+ occupation=str(params.get('occupation', self._get_attr('occupation', 'Professional'))),
120
+ city=str(params.get('city', self._get_attr('residence', 'Unknown'))),
121
+ country=str(params.get('country', self._get_attr('nationality', 'Unknown'))),
122
+ custom_values=str(params.get('custom_values', self._get_attr('beliefs', 'Hardworking'))),
123
+ custom_life_attitude=str(params.get('custom_life_attitude', self._get_attr('personality', 'Positive'))),
124
+ life_story=str(params.get('life_story', self._get_attr('other_facts', 'Standard story'))),
125
+ interests_hobbies=str(params.get('interests_hobbies', self._get_attr('preferences', 'Reading'))),
126
+ attribute_count=float(params.get('attribute_count', 350.0)),
127
+ api_name="/generate_persona"
128
+ )
129
+
130
+ # The result from DeepPersona is raw string. Let's merge it into the profile text.
131
+ self._merge_persona_data({"full_profile_text": result})
132
+ return self
133
+
134
+ def _get_attr(self, key, default):
135
+ return getattr(self, '_persona', {}).get(key, default)
136
+
137
+ def _merge_persona_data(self, api_data: dict):
138
+ for key, value in api_data.items():
139
+ if hasattr(self, f'_{key}'):
140
+ setattr(self, f'_{key}', value)
141
+ else:
142
+ self._persona[key] = value
143
+
144
+ return APIEnhancedTinyPerson
145
+
146
+ # =========================================================================
147
+
148
  def generate_personas(business_description, customer_profile, num_personas, api_key=None):
149
  if api_key:
150
  os.environ["OPENAI_API_KEY"] = api_key
 
409
 
410
  def generate_personas_router(business_description, customer_profile, num_personas, method, example_file, api_key=None):
411
  if method == "DeepPersona":
412
+ CONFIG["persona_generation"]["mode"] = "hybrid"
413
+ return create_persona("Dynamic", business_description, customer_profile, num_personas, api_key)
414
  elif method == "TinyTroupe":
415
  if api_key:
416
  os.environ["OPENAI_API_KEY"] = api_key