Spaces:
Paused
Paused
Upload folder using huggingface_hub
Browse files- Dockerfile +23 -0
- README.md +2 -3
- app.py +6 -51
Dockerfile
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.10-slim
|
| 2 |
+
|
| 3 |
+
WORKDIR /app
|
| 4 |
+
|
| 5 |
+
RUN apt-get update && apt-get install -y \
|
| 6 |
+
build-essential \
|
| 7 |
+
curl \
|
| 8 |
+
git \
|
| 9 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 10 |
+
|
| 11 |
+
COPY requirements.txt .
|
| 12 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 13 |
+
|
| 14 |
+
COPY . .
|
| 15 |
+
|
| 16 |
+
# Set environment variables
|
| 17 |
+
ENV PYTHONUNBUFFERED=1
|
| 18 |
+
ENV GRADIO_SERVER_NAME="0.0.0.0"
|
| 19 |
+
ENV GRADIO_SERVER_PORT=7860
|
| 20 |
+
|
| 21 |
+
EXPOSE 7860
|
| 22 |
+
|
| 23 |
+
CMD ["python", "app.py"]
|
README.md
CHANGED
|
@@ -3,9 +3,8 @@ title: Tiny Factory
|
|
| 3 |
emoji: 💻
|
| 4 |
colorFrom: yellow
|
| 5 |
colorTo: gray
|
| 6 |
-
sdk:
|
| 7 |
-
|
| 8 |
-
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
---
|
| 11 |
|
|
|
|
| 3 |
emoji: 💻
|
| 4 |
colorFrom: yellow
|
| 5 |
colorTo: gray
|
| 6 |
+
sdk: docker
|
| 7 |
+
app_port: 7860
|
|
|
|
| 8 |
pinned: false
|
| 9 |
---
|
| 10 |
|
app.py
CHANGED
|
@@ -42,80 +42,44 @@ def save_persona_base(personas):
|
|
| 42 |
except Exception as e:
|
| 43 |
print(f"Error saving persona base to Hub: {e}")
|
| 44 |
|
| 45 |
-
# --- CHANGE 1: The function now accepts an optional API key. ---
|
| 46 |
def generate_personas(business_description, customer_profile, num_personas, blablador_api_key=None):
|
| 47 |
-
"""
|
| 48 |
-
Generates a list of TinyPerson instances based on the provided inputs.
|
| 49 |
-
It prioritizes the API key passed as an argument, but falls back to the
|
| 50 |
-
environment variable if none is provided (for UI use).
|
| 51 |
-
"""
|
| 52 |
-
# --- CHANGE 2: Logic to determine which key to use. ---
|
| 53 |
-
# Use the key from the API call if provided, otherwise get it from the Space secrets.
|
| 54 |
api_key_to_use = blablador_api_key or os.getenv("BLABLADOR_API_KEY")
|
| 55 |
-
|
| 56 |
if not api_key_to_use:
|
| 57 |
return {"error": "BLABLADOR_API_KEY not found. Please provide it in your API call or set it as a secret in the Space settings."}
|
| 58 |
-
|
| 59 |
-
# Store the original state of the environment variable, if it exists
|
| 60 |
original_key = os.getenv("BLABLADOR_API_KEY")
|
| 61 |
-
|
| 62 |
try:
|
| 63 |
-
# --- CHANGE 3: Securely set the correct environment variable for this request. ---
|
| 64 |
-
# The underlying tinytroupe library will look for this variable.
|
| 65 |
os.environ["BLABLADOR_API_KEY"] = api_key_to_use
|
| 66 |
-
|
| 67 |
num_personas = int(num_personas)
|
| 68 |
-
|
| 69 |
factory = TinyPersonFactory(
|
| 70 |
context=business_description,
|
| 71 |
sampling_space_description=customer_profile,
|
| 72 |
total_population_size=num_personas
|
| 73 |
)
|
| 74 |
-
|
| 75 |
people = factory.generate_people(number_of_people=num_personas, parallelize=False)
|
| 76 |
personas_data = [person._persona for person in people]
|
| 77 |
-
|
| 78 |
-
# --- NEW: Update the Tresor ---
|
| 79 |
current_base = load_persona_base()
|
| 80 |
current_base.extend(personas_data)
|
| 81 |
save_persona_base(current_base)
|
| 82 |
-
# ------------------------------
|
| 83 |
-
|
| 84 |
return personas_data
|
| 85 |
-
|
| 86 |
except Exception as e:
|
| 87 |
return {"error": str(e)}
|
| 88 |
-
|
| 89 |
finally:
|
| 90 |
-
# --- CHANGE 4: A robust cleanup using a 'finally' block. ---
|
| 91 |
-
# This ensures the environment is always restored to its original state,
|
| 92 |
-
# whether the function succeeds or fails.
|
| 93 |
if original_key is None:
|
| 94 |
-
# If the variable didn't exist originally, remove it.
|
| 95 |
if "BLABLADOR_API_KEY" in os.environ:
|
| 96 |
del os.environ["BLABLADOR_API_KEY"]
|
| 97 |
else:
|
| 98 |
-
# If it existed, restore its original value.
|
| 99 |
os.environ["BLABLADOR_API_KEY"] = original_key
|
| 100 |
|
| 101 |
-
|
| 102 |
def find_best_persona(criteria):
|
| 103 |
-
"""
|
| 104 |
-
Loads the persona base and finds the best matching persona based on criteria.
|
| 105 |
-
"""
|
| 106 |
personas = load_persona_base()
|
| 107 |
if not personas:
|
| 108 |
return {"error": "Persona base is empty. Generate some personas first!"}
|
| 109 |
-
|
| 110 |
try:
|
| 111 |
-
# select_best_persona uses LLM to find the best index
|
| 112 |
idx = select_best_persona(criteria=criteria, personas=personas)
|
| 113 |
-
|
| 114 |
try:
|
| 115 |
idx = int(idx)
|
| 116 |
except (ValueError, TypeError):
|
| 117 |
return {"error": f"LLM returned an invalid index: {idx}"}
|
| 118 |
-
|
| 119 |
if idx >= 0 and idx < len(personas):
|
| 120 |
return personas[idx]
|
| 121 |
else:
|
|
@@ -123,7 +87,6 @@ def find_best_persona(criteria):
|
|
| 123 |
except Exception as e:
|
| 124 |
return {"error": f"Error during persona matching: {str(e)}"}
|
| 125 |
|
| 126 |
-
|
| 127 |
with gr.Blocks() as demo:
|
| 128 |
gr.Markdown("<h1>Tiny Persona Generator</h1>")
|
| 129 |
with gr.Row():
|
|
@@ -131,32 +94,20 @@ with gr.Blocks() as demo:
|
|
| 131 |
business_description_input = gr.Textbox(label="What is your business about?", lines=5)
|
| 132 |
customer_profile_input = gr.Textbox(label="Information about your customer profile", lines=5)
|
| 133 |
num_personas_input = gr.Number(label="Number of personas to generate", value=1, minimum=1, step=1)
|
| 134 |
-
|
| 135 |
-
# --- CHANGE 5: The API key input is now INVISIBLE. ---
|
| 136 |
-
# It still exists, so the API endpoint is created, but it's hidden from UI users.
|
| 137 |
-
blablador_api_key_input = gr.Textbox(
|
| 138 |
-
label="Blablador API Key (for API client use)",
|
| 139 |
-
visible=False
|
| 140 |
-
)
|
| 141 |
-
|
| 142 |
generate_button = gr.Button("Generate Personas")
|
| 143 |
-
|
| 144 |
gr.Markdown("---")
|
| 145 |
gr.Markdown("<h3>Search Tresor</h3>")
|
| 146 |
criteria_input = gr.Textbox(label="Criteria to find best matching persona", lines=2)
|
| 147 |
find_button = gr.Button("Find Best Persona in Tresor")
|
| 148 |
-
|
| 149 |
with gr.Column():
|
| 150 |
output_json = gr.JSON(label="Output (Generated or Matched Persona)")
|
| 151 |
-
|
| 152 |
generate_button.click(
|
| 153 |
fn=generate_personas,
|
| 154 |
-
# --- CHANGE 6: Pass the invisible textbox to the function. ---
|
| 155 |
inputs=[business_description_input, customer_profile_input, num_personas_input, blablador_api_key_input],
|
| 156 |
outputs=output_json,
|
| 157 |
api_name="generate_personas"
|
| 158 |
)
|
| 159 |
-
|
| 160 |
find_button.click(
|
| 161 |
fn=find_best_persona,
|
| 162 |
inputs=[criteria_input],
|
|
@@ -174,7 +125,11 @@ def health():
|
|
| 174 |
def api_docs():
|
| 175 |
return RedirectResponse(url="/docs")
|
| 176 |
|
| 177 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 178 |
|
| 179 |
if __name__ == "__main__":
|
| 180 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 42 |
except Exception as e:
|
| 43 |
print(f"Error saving persona base to Hub: {e}")
|
| 44 |
|
|
|
|
| 45 |
def generate_personas(business_description, customer_profile, num_personas, blablador_api_key=None):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
api_key_to_use = blablador_api_key or os.getenv("BLABLADOR_API_KEY")
|
|
|
|
| 47 |
if not api_key_to_use:
|
| 48 |
return {"error": "BLABLADOR_API_KEY not found. Please provide it in your API call or set it as a secret in the Space settings."}
|
|
|
|
|
|
|
| 49 |
original_key = os.getenv("BLABLADOR_API_KEY")
|
|
|
|
| 50 |
try:
|
|
|
|
|
|
|
| 51 |
os.environ["BLABLADOR_API_KEY"] = api_key_to_use
|
|
|
|
| 52 |
num_personas = int(num_personas)
|
|
|
|
| 53 |
factory = TinyPersonFactory(
|
| 54 |
context=business_description,
|
| 55 |
sampling_space_description=customer_profile,
|
| 56 |
total_population_size=num_personas
|
| 57 |
)
|
|
|
|
| 58 |
people = factory.generate_people(number_of_people=num_personas, parallelize=False)
|
| 59 |
personas_data = [person._persona for person in people]
|
|
|
|
|
|
|
| 60 |
current_base = load_persona_base()
|
| 61 |
current_base.extend(personas_data)
|
| 62 |
save_persona_base(current_base)
|
|
|
|
|
|
|
| 63 |
return personas_data
|
|
|
|
| 64 |
except Exception as e:
|
| 65 |
return {"error": str(e)}
|
|
|
|
| 66 |
finally:
|
|
|
|
|
|
|
|
|
|
| 67 |
if original_key is None:
|
|
|
|
| 68 |
if "BLABLADOR_API_KEY" in os.environ:
|
| 69 |
del os.environ["BLABLADOR_API_KEY"]
|
| 70 |
else:
|
|
|
|
| 71 |
os.environ["BLABLADOR_API_KEY"] = original_key
|
| 72 |
|
|
|
|
| 73 |
def find_best_persona(criteria):
|
|
|
|
|
|
|
|
|
|
| 74 |
personas = load_persona_base()
|
| 75 |
if not personas:
|
| 76 |
return {"error": "Persona base is empty. Generate some personas first!"}
|
|
|
|
| 77 |
try:
|
|
|
|
| 78 |
idx = select_best_persona(criteria=criteria, personas=personas)
|
|
|
|
| 79 |
try:
|
| 80 |
idx = int(idx)
|
| 81 |
except (ValueError, TypeError):
|
| 82 |
return {"error": f"LLM returned an invalid index: {idx}"}
|
|
|
|
| 83 |
if idx >= 0 and idx < len(personas):
|
| 84 |
return personas[idx]
|
| 85 |
else:
|
|
|
|
| 87 |
except Exception as e:
|
| 88 |
return {"error": f"Error during persona matching: {str(e)}"}
|
| 89 |
|
|
|
|
| 90 |
with gr.Blocks() as demo:
|
| 91 |
gr.Markdown("<h1>Tiny Persona Generator</h1>")
|
| 92 |
with gr.Row():
|
|
|
|
| 94 |
business_description_input = gr.Textbox(label="What is your business about?", lines=5)
|
| 95 |
customer_profile_input = gr.Textbox(label="Information about your customer profile", lines=5)
|
| 96 |
num_personas_input = gr.Number(label="Number of personas to generate", value=1, minimum=1, step=1)
|
| 97 |
+
blablador_api_key_input = gr.Textbox(label="Blablador API Key (for API client use)", visible=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
generate_button = gr.Button("Generate Personas")
|
|
|
|
| 99 |
gr.Markdown("---")
|
| 100 |
gr.Markdown("<h3>Search Tresor</h3>")
|
| 101 |
criteria_input = gr.Textbox(label="Criteria to find best matching persona", lines=2)
|
| 102 |
find_button = gr.Button("Find Best Persona in Tresor")
|
|
|
|
| 103 |
with gr.Column():
|
| 104 |
output_json = gr.JSON(label="Output (Generated or Matched Persona)")
|
|
|
|
| 105 |
generate_button.click(
|
| 106 |
fn=generate_personas,
|
|
|
|
| 107 |
inputs=[business_description_input, customer_profile_input, num_personas_input, blablador_api_key_input],
|
| 108 |
outputs=output_json,
|
| 109 |
api_name="generate_personas"
|
| 110 |
)
|
|
|
|
| 111 |
find_button.click(
|
| 112 |
fn=find_best_persona,
|
| 113 |
inputs=[criteria_input],
|
|
|
|
| 125 |
def api_docs():
|
| 126 |
return RedirectResponse(url="/docs")
|
| 127 |
|
| 128 |
+
# Disabling SSR to fix the port 7861 error in Spaces
|
| 129 |
+
try:
|
| 130 |
+
app = gr.mount_gradio_app(app, demo, path="/", ssr=False)
|
| 131 |
+
except Exception:
|
| 132 |
+
app = gr.mount_gradio_app(app, demo, path="/")
|
| 133 |
|
| 134 |
if __name__ == "__main__":
|
| 135 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|