Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
import random
|
| 4 |
+
|
| 5 |
+
# Initialize the Hugging Face text generation pipeline with distilgpt2
|
| 6 |
+
generator = pipeline("text-generation", model="distilgpt2")
|
| 7 |
+
|
| 8 |
+
# Function to generate checklists, tips, and engagement score
|
| 9 |
+
def generate_project_data(project_input):
|
| 10 |
+
# Generate checklists (3 tasks)
|
| 11 |
+
checklist_prompt = f"Generate a list of 3 safety and productivity tasks for a construction project: {project_input}"
|
| 12 |
+
checklist_response = generator(checklist_prompt, max_length=100, num_return_sequences=1, truncation=True)[0]["generated_text"]
|
| 13 |
+
# Extract tasks (simple parsing assuming the model returns a list-like structure)
|
| 14 |
+
tasks = checklist_response.replace(checklist_prompt, "").split(".")[:3]
|
| 15 |
+
tasks = [task.strip() for task in tasks if task.strip()]
|
| 16 |
+
if len(tasks) < 3:
|
| 17 |
+
# Fallback tasks if the model doesn't generate enough
|
| 18 |
+
tasks.extend([
|
| 19 |
+
"Conduct a safety briefing with the team.",
|
| 20 |
+
"Inspect all equipment before use.",
|
| 21 |
+
"Ensure all workers are wearing PPE."
|
| 22 |
+
][:3 - len(tasks)])
|
| 23 |
+
|
| 24 |
+
# Generate a tip
|
| 25 |
+
tip_prompt = f"Provide a productivity tip for a construction project supervisor: {project_input}"
|
| 26 |
+
tip_response = generator(tip_prompt, max_length=50, num_return_sequences=1, truncation=True)[0]["generated_text"]
|
| 27 |
+
tip = tip_response.replace(tip_prompt, "").strip()
|
| 28 |
+
if not tip:
|
| 29 |
+
tip = "Schedule regular breaks to maintain team focus."
|
| 30 |
+
|
| 31 |
+
# Generate a mock engagement score (rule-based for simplicity)
|
| 32 |
+
# In a real scenario, this could be generated by a model trained on engagement data
|
| 33 |
+
engagement_score = random.randint(70, 90) # Random score between 70 and 90
|
| 34 |
+
|
| 35 |
+
# Return the data in the expected JSON format
|
| 36 |
+
return {
|
| 37 |
+
"checklists": [{"task": task} for task in tasks],
|
| 38 |
+
"tips": tip,
|
| 39 |
+
"engagementScore": engagement_score
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
# Create a Gradio interface
|
| 43 |
+
interface = gr.Interface(
|
| 44 |
+
fn=generate_project_data,
|
| 45 |
+
inputs=gr.Textbox(label="Project Input", placeholder="Enter project details (e.g., Project: Highway Construction, Start Date: 2025-05-01)"),
|
| 46 |
+
outputs=gr.JSON(label="Generated Data"),
|
| 47 |
+
title="AI Coach Data Generator",
|
| 48 |
+
description="Generates daily checklists, tips, and engagement scores for construction projects."
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
# Launch the app
|
| 52 |
+
interface.launch()
|