Spaces:
Paused
Paused
Upload folder using huggingface_hub
Browse files- app.py +30 -19
- test_remote_api.py +27 -0
app.py
CHANGED
|
@@ -34,6 +34,7 @@ try:
|
|
| 34 |
import tinytroupe
|
| 35 |
from tinytroupe.agent import TinyPerson
|
| 36 |
from tinytroupe.factory.tiny_person_factory import TinyPersonFactory
|
|
|
|
| 37 |
print("TinyTroupe imported successfully")
|
| 38 |
except ImportError as e:
|
| 39 |
print(f"Error importing TinyTroupe: {e}")
|
|
@@ -82,6 +83,10 @@ def get_repo_branches(repo_full_name):
|
|
| 82 |
return ["main"]
|
| 83 |
|
| 84 |
def generate_personas(theme, customer_profile, num_personas):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
context = f"A company related to {theme}. Target customers: {customer_profile}"
|
| 86 |
factory = TinyPersonFactory(context=context)
|
| 87 |
people = factory.generate_people(number_of_people=int(num_personas), verbose=True)
|
|
@@ -111,27 +116,33 @@ def generate_tasks(theme, customer_profile):
|
|
| 111 |
4. Emotional connection to the persona and content/styling
|
| 112 |
|
| 113 |
The tasks must be in sequential order.
|
| 114 |
-
Return the tasks as a JSON list of strings.
|
| 115 |
"""
|
| 116 |
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
|
| 136 |
def handle_generate(theme, customer_profile, num_personas):
|
| 137 |
try:
|
|
|
|
| 34 |
import tinytroupe
|
| 35 |
from tinytroupe.agent import TinyPerson
|
| 36 |
from tinytroupe.factory.tiny_person_factory import TinyPersonFactory
|
| 37 |
+
from tinytroupe import config_manager
|
| 38 |
print("TinyTroupe imported successfully")
|
| 39 |
except ImportError as e:
|
| 40 |
print(f"Error importing TinyTroupe: {e}")
|
|
|
|
| 83 |
return ["main"]
|
| 84 |
|
| 85 |
def generate_personas(theme, customer_profile, num_personas):
|
| 86 |
+
# Override model if alias-large is failing
|
| 87 |
+
config_manager.update("model", "alias-fast")
|
| 88 |
+
config_manager.update("reasoning_model", "alias-fast")
|
| 89 |
+
|
| 90 |
context = f"A company related to {theme}. Target customers: {customer_profile}"
|
| 91 |
factory = TinyPersonFactory(context=context)
|
| 92 |
people = factory.generate_people(number_of_people=int(num_personas), verbose=True)
|
|
|
|
| 116 |
4. Emotional connection to the persona and content/styling
|
| 117 |
|
| 118 |
The tasks must be in sequential order.
|
| 119 |
+
Return the tasks as a JSON list of strings in the format: {{"tasks": ["task1", "task2", ...]}}
|
| 120 |
"""
|
| 121 |
|
| 122 |
+
# Try alias-large first, then alias-fast
|
| 123 |
+
for model_name in ["alias-large", "alias-fast"]:
|
| 124 |
+
try:
|
| 125 |
+
response = client.chat.completions.create(
|
| 126 |
+
model=model_name,
|
| 127 |
+
messages=[{"role": "user", "content": prompt}]
|
| 128 |
+
# Removed response_format="json_object" as it might not be supported
|
| 129 |
+
)
|
| 130 |
+
content = response.choices[0].message.content
|
| 131 |
+
# Extract JSON from content (it might be wrapped in code blocks)
|
| 132 |
+
json_str = re.search(r"\{.*\}", content, re.DOTALL)
|
| 133 |
+
if json_str:
|
| 134 |
+
tasks_json = json.loads(json_str.group())
|
| 135 |
+
if "tasks" in tasks_json:
|
| 136 |
+
return tasks_json["tasks"]
|
| 137 |
+
elif isinstance(tasks_json, list):
|
| 138 |
+
return tasks_json
|
| 139 |
+
else:
|
| 140 |
+
return list(tasks_json.values())[0]
|
| 141 |
+
except Exception as e:
|
| 142 |
+
print(f"Error with model {model_name}: {e}")
|
| 143 |
+
continue
|
| 144 |
+
|
| 145 |
+
return [f"Task {i+1} for {theme} (Failed to generate)" for i in range(10)]
|
| 146 |
|
| 147 |
def handle_generate(theme, customer_profile, num_personas):
|
| 148 |
try:
|
test_remote_api.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from gradio_client import Client
|
| 2 |
+
import json
|
| 3 |
+
|
| 4 |
+
client = Client("https://harvesthealth-xxg-backup.hf.space/")
|
| 5 |
+
|
| 6 |
+
print("--- Testing /get_repo_branches ---")
|
| 7 |
+
try:
|
| 8 |
+
result = client.predict(
|
| 9 |
+
repo_full_name="JsonLord/tiny_web",
|
| 10 |
+
api_name="/get_repo_branches"
|
| 11 |
+
)
|
| 12 |
+
print(f"Result: {result}")
|
| 13 |
+
except Exception as e:
|
| 14 |
+
print(f"Error: {e}")
|
| 15 |
+
|
| 16 |
+
print("\n--- Testing /handle_generate ---")
|
| 17 |
+
try:
|
| 18 |
+
# Using small values for quick test
|
| 19 |
+
result = client.predict(
|
| 20 |
+
theme="Education",
|
| 21 |
+
customer_profile="Student looking for online courses",
|
| 22 |
+
num_personas=1,
|
| 23 |
+
api_name="/handle_generate"
|
| 24 |
+
)
|
| 25 |
+
print(f"Result: {result}")
|
| 26 |
+
except Exception as e:
|
| 27 |
+
print(f"Error: {e}")
|