Spaces:
Paused
Paused
Upload folder using huggingface_hub
Browse files- app.py +94 -12
- patch_app.py +111 -0
app.py
CHANGED
|
@@ -15,18 +15,100 @@ simulation_manager = SimulationManager()
|
|
| 15 |
REMOTE_BACKEND = "https://auxteam-tiny-factory.hf.space"
|
| 16 |
|
| 17 |
def generate_personas(business_description, customer_profile, num_personas, api_key=None):
|
| 18 |
-
if api_key:
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
def start_simulation(name, content_text, format_type, persona_count, network_type):
|
| 31 |
config = SimulationConfig(name=name, persona_count=int(persona_count), network_type=network_type)
|
| 32 |
sim = simulation_manager.create_simulation(config)
|
|
@@ -213,4 +295,4 @@ with gr.Blocks(css=".big-input textarea { height: 300px !important; } #mesh-netw
|
|
| 213 |
gr.Button("save_focus_group").click(save_focus_group_api, inputs=[gr.Textbox(), gr.Textbox()], outputs=gr.JSON(), api_name="save_focus_group")
|
| 214 |
|
| 215 |
if __name__ == "__main__":
|
| 216 |
-
demo.launch()
|
|
|
|
| 15 |
REMOTE_BACKEND = "https://auxteam-tiny-factory.hf.space"
|
| 16 |
|
| 17 |
def generate_personas(business_description, customer_profile, num_personas, api_key=None):
|
| 18 |
+
if api_key:
|
| 19 |
+
os.environ["BLABLADOR_API_KEY"] = api_key
|
| 20 |
+
os.environ["OPENAI_API_KEY"] = api_key
|
| 21 |
+
|
| 22 |
+
import json
|
| 23 |
+
from gradio_client import Client
|
| 24 |
+
import openai
|
| 25 |
+
|
| 26 |
+
client = openai.OpenAI()
|
| 27 |
+
dp_client = Client("THzva/deeppersona-experience")
|
| 28 |
+
|
| 29 |
+
personas = []
|
| 30 |
+
|
| 31 |
+
for _ in range(int(num_personas)):
|
| 32 |
+
# 1. Generate initial parameters for the 200 API call
|
| 33 |
+
prompt_1 = f"""
|
| 34 |
+
Given the following business description and customer profile:
|
| 35 |
+
Business: {business_description}
|
| 36 |
+
Customer: {customer_profile}
|
| 37 |
+
|
| 38 |
+
Generate realistic parameters for a persona that fits this profile. Return ONLY a valid JSON object with these EXACT keys:
|
| 39 |
+
"Age" (number), "Gender" (string), "Occupation" (string), "City" (string), "Country" (string), "Personal Values" (string), "Life Attitude" (string), "Life Story" (string), "Interests and Hobbies" (string).
|
| 40 |
+
Keep the string fields concise (1-2 sentences).
|
| 41 |
+
"""
|
| 42 |
+
response_1 = client.chat.completions.create(
|
| 43 |
+
model="gpt-4o-mini",
|
| 44 |
+
messages=[{"role": "user", "content": prompt_1}],
|
| 45 |
+
response_format={"type": "json_object"}
|
| 46 |
+
)
|
| 47 |
+
params_1 = json.loads(response_1.choices[0].message.content)
|
| 48 |
+
|
| 49 |
+
# 2. Call DeepPersona with 200 attributes
|
| 50 |
+
result_200 = dp_client.predict(
|
| 51 |
+
age=params_1.get("Age", 30),
|
| 52 |
+
gender=params_1.get("Gender", "Female"),
|
| 53 |
+
occupation=params_1.get("Occupation", "Professional"),
|
| 54 |
+
city=params_1.get("City", "New York"),
|
| 55 |
+
country=params_1.get("Country", "USA"),
|
| 56 |
+
custom_values=params_1.get("Personal Values", "Hardworking"),
|
| 57 |
+
custom_life_attitude=params_1.get("Life Attitude", "Positive"),
|
| 58 |
+
life_story=params_1.get("Life Story", "Grew up in the city"),
|
| 59 |
+
interests_hobbies=params_1.get("Interests and Hobbies", "Reading"),
|
| 60 |
+
attribute_count=200,
|
| 61 |
+
api_name="/generate_persona"
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
# 3. Use LLM to extract specific truth/details from 200 output for the 400 call
|
| 65 |
+
prompt_2 = f"""
|
| 66 |
+
Based on this generated persona output:
|
| 67 |
+
{result_200}
|
| 68 |
+
|
| 69 |
+
Extract and enhance specific details to create an updated set of parameters. Return ONLY a valid JSON object with these EXACT keys:
|
| 70 |
+
"Age" (number), "Gender" (string), "Occupation" (string), "City" (string), "Country" (string), "Personal Values" (string), "Life Attitude" (string), "Life Story" (string), "Interests and Hobbies" (string).
|
| 71 |
+
"""
|
| 72 |
+
response_2 = client.chat.completions.create(
|
| 73 |
+
model="gpt-4o-mini",
|
| 74 |
+
messages=[{"role": "user", "content": prompt_2}],
|
| 75 |
+
response_format={"type": "json_object"}
|
| 76 |
+
)
|
| 77 |
+
params_2 = json.loads(response_2.choices[0].message.content)
|
| 78 |
+
|
| 79 |
+
# 4. Call DeepPersona with 400 attributes
|
| 80 |
+
result_400 = dp_client.predict(
|
| 81 |
+
age=params_2.get("Age", 30),
|
| 82 |
+
gender=params_2.get("Gender", "Female"),
|
| 83 |
+
occupation=params_2.get("Occupation", "Professional"),
|
| 84 |
+
city=params_2.get("City", "New York"),
|
| 85 |
+
country=params_2.get("Country", "USA"),
|
| 86 |
+
custom_values=params_2.get("Personal Values", "Hardworking"),
|
| 87 |
+
custom_life_attitude=params_2.get("Life Attitude", "Positive"),
|
| 88 |
+
life_story=params_2.get("Life Story", "Grew up in the city"),
|
| 89 |
+
interests_hobbies=params_2.get("Interests and Hobbies", "Reading"),
|
| 90 |
+
attribute_count=400,
|
| 91 |
+
api_name="/generate_persona"
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
# 5. Extract final structured data for _persona output
|
| 95 |
+
prompt_3 = f"""
|
| 96 |
+
Based on this final generated persona output:
|
| 97 |
+
{result_400}
|
| 98 |
|
| 99 |
+
Extract the persona details into a structured format. Return ONLY a valid JSON object with these EXACT keys:
|
| 100 |
+
"name" (string, make one up if not found), "age" (number), "nationality" (string), "country_of_residence" (string), "occupation" (string), "residence" (string).
|
| 101 |
+
"""
|
| 102 |
+
response_3 = client.chat.completions.create(
|
| 103 |
+
model="gpt-4o-mini",
|
| 104 |
+
messages=[{"role": "user", "content": prompt_3}],
|
| 105 |
+
response_format={"type": "json_object"}
|
| 106 |
+
)
|
| 107 |
+
final_persona = json.loads(response_3.choices[0].message.content)
|
| 108 |
+
final_persona["full_profile_text"] = result_400
|
| 109 |
+
personas.append(final_persona)
|
| 110 |
+
|
| 111 |
+
return personas
|
| 112 |
def start_simulation(name, content_text, format_type, persona_count, network_type):
|
| 113 |
config = SimulationConfig(name=name, persona_count=int(persona_count), network_type=network_type)
|
| 114 |
sim = simulation_manager.create_simulation(config)
|
|
|
|
| 295 |
gr.Button("save_focus_group").click(save_focus_group_api, inputs=[gr.Textbox(), gr.Textbox()], outputs=gr.JSON(), api_name="save_focus_group")
|
| 296 |
|
| 297 |
if __name__ == "__main__":
|
| 298 |
+
demo.launch(show_error=True)
|
patch_app.py
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import re
|
| 2 |
+
|
| 3 |
+
with open('app.py', 'r') as f:
|
| 4 |
+
content = f.read()
|
| 5 |
+
|
| 6 |
+
new_func = '''
|
| 7 |
+
def generate_personas(business_description, customer_profile, num_personas, api_key=None):
|
| 8 |
+
if api_key:
|
| 9 |
+
os.environ["BLABLADOR_API_KEY"] = api_key
|
| 10 |
+
os.environ["OPENAI_API_KEY"] = api_key
|
| 11 |
+
|
| 12 |
+
import json
|
| 13 |
+
from gradio_client import Client
|
| 14 |
+
import openai
|
| 15 |
+
|
| 16 |
+
client = openai.OpenAI()
|
| 17 |
+
dp_client = Client("THzva/deeppersona-experience")
|
| 18 |
+
|
| 19 |
+
personas = []
|
| 20 |
+
|
| 21 |
+
for _ in range(int(num_personas)):
|
| 22 |
+
# 1. Generate initial parameters for the 200 API call
|
| 23 |
+
prompt_1 = f"""
|
| 24 |
+
Given the following business description and customer profile:
|
| 25 |
+
Business: {business_description}
|
| 26 |
+
Customer: {customer_profile}
|
| 27 |
+
|
| 28 |
+
Generate realistic parameters for a persona that fits this profile. Return ONLY a valid JSON object with these EXACT keys:
|
| 29 |
+
"Age" (number), "Gender" (string), "Occupation" (string), "City" (string), "Country" (string), "Personal Values" (string), "Life Attitude" (string), "Life Story" (string), "Interests and Hobbies" (string).
|
| 30 |
+
Keep the string fields concise (1-2 sentences).
|
| 31 |
+
"""
|
| 32 |
+
response_1 = client.chat.completions.create(
|
| 33 |
+
model="gpt-4o-mini",
|
| 34 |
+
messages=[{"role": "user", "content": prompt_1}],
|
| 35 |
+
response_format={"type": "json_object"}
|
| 36 |
+
)
|
| 37 |
+
params_1 = json.loads(response_1.choices[0].message.content)
|
| 38 |
+
|
| 39 |
+
# 2. Call DeepPersona with 200 attributes
|
| 40 |
+
result_200 = dp_client.predict(
|
| 41 |
+
age=params_1.get("Age", 30),
|
| 42 |
+
gender=params_1.get("Gender", "Female"),
|
| 43 |
+
occupation=params_1.get("Occupation", "Professional"),
|
| 44 |
+
city=params_1.get("City", "New York"),
|
| 45 |
+
country=params_1.get("Country", "USA"),
|
| 46 |
+
custom_values=params_1.get("Personal Values", "Hardworking"),
|
| 47 |
+
custom_life_attitude=params_1.get("Life Attitude", "Positive"),
|
| 48 |
+
life_story=params_1.get("Life Story", "Grew up in the city"),
|
| 49 |
+
interests_hobbies=params_1.get("Interests and Hobbies", "Reading"),
|
| 50 |
+
attribute_count=200,
|
| 51 |
+
api_name="/generate_persona"
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
# 3. Use LLM to extract specific truth/details from 200 output for the 400 call
|
| 55 |
+
prompt_2 = f"""
|
| 56 |
+
Based on this generated persona output:
|
| 57 |
+
{result_200}
|
| 58 |
+
|
| 59 |
+
Extract and enhance specific details to create an updated set of parameters. Return ONLY a valid JSON object with these EXACT keys:
|
| 60 |
+
"Age" (number), "Gender" (string), "Occupation" (string), "City" (string), "Country" (string), "Personal Values" (string), "Life Attitude" (string), "Life Story" (string), "Interests and Hobbies" (string).
|
| 61 |
+
"""
|
| 62 |
+
response_2 = client.chat.completions.create(
|
| 63 |
+
model="gpt-4o-mini",
|
| 64 |
+
messages=[{"role": "user", "content": prompt_2}],
|
| 65 |
+
response_format={"type": "json_object"}
|
| 66 |
+
)
|
| 67 |
+
params_2 = json.loads(response_2.choices[0].message.content)
|
| 68 |
+
|
| 69 |
+
# 4. Call DeepPersona with 400 attributes
|
| 70 |
+
result_400 = dp_client.predict(
|
| 71 |
+
age=params_2.get("Age", 30),
|
| 72 |
+
gender=params_2.get("Gender", "Female"),
|
| 73 |
+
occupation=params_2.get("Occupation", "Professional"),
|
| 74 |
+
city=params_2.get("City", "New York"),
|
| 75 |
+
country=params_2.get("Country", "USA"),
|
| 76 |
+
custom_values=params_2.get("Personal Values", "Hardworking"),
|
| 77 |
+
custom_life_attitude=params_2.get("Life Attitude", "Positive"),
|
| 78 |
+
life_story=params_2.get("Life Story", "Grew up in the city"),
|
| 79 |
+
interests_hobbies=params_2.get("Interests and Hobbies", "Reading"),
|
| 80 |
+
attribute_count=400,
|
| 81 |
+
api_name="/generate_persona"
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
# 5. Extract final structured data for _persona output
|
| 85 |
+
prompt_3 = f"""
|
| 86 |
+
Based on this final generated persona output:
|
| 87 |
+
{result_400}
|
| 88 |
+
|
| 89 |
+
Extract the persona details into a structured format. Return ONLY a valid JSON object with these EXACT keys:
|
| 90 |
+
"name" (string, make one up if not found), "age" (number), "nationality" (string), "country_of_residence" (string), "occupation" (string), "residence" (string).
|
| 91 |
+
"""
|
| 92 |
+
response_3 = client.chat.completions.create(
|
| 93 |
+
model="gpt-4o-mini",
|
| 94 |
+
messages=[{"role": "user", "content": prompt_3}],
|
| 95 |
+
response_format={"type": "json_object"}
|
| 96 |
+
)
|
| 97 |
+
final_persona = json.loads(response_3.choices[0].message.content)
|
| 98 |
+
final_persona["full_profile_text"] = result_400
|
| 99 |
+
personas.append(final_persona)
|
| 100 |
+
|
| 101 |
+
return personas
|
| 102 |
+
'''
|
| 103 |
+
|
| 104 |
+
# Find the def generate_personas function block
|
| 105 |
+
pattern = r"def generate_personas\(business_description, customer_profile, num_personas, api_key=None\):.*?(?=\ndef start_simulation)"
|
| 106 |
+
new_content = re.sub(pattern, new_func.strip(), content, flags=re.DOTALL)
|
| 107 |
+
|
| 108 |
+
with open('app.py', 'w') as f:
|
| 109 |
+
f.write(new_content)
|
| 110 |
+
|
| 111 |
+
print("Patch applied")
|