Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
from simple_salesforce import Salesforce
|
4 |
+
import datetime
|
5 |
+
import os
|
6 |
+
from dotenv import load_dotenv
|
7 |
+
|
8 |
+
# Load environment variables from .env file
|
9 |
+
load_dotenv()
|
10 |
+
|
11 |
+
# Initialize Hugging Face model
|
12 |
+
generator = pipeline("text-generation", model="distilgpt2")
|
13 |
+
|
14 |
+
# Initialize Salesforce connection using environment variables
|
15 |
+
sf = Salesforce(
|
16 |
+
username=os.getenv("SF_USERNAME"),
|
17 |
+
password=os.getenv("SF_PASSWORD"),
|
18 |
+
security_token=os.getenv("SF_SECURITY_TOKEN")
|
19 |
+
)
|
20 |
+
|
21 |
+
def generate_ai_data(supervisor_id, project_id, supervisor_data, project_data):
|
22 |
+
"""
|
23 |
+
Generate AI coaching data and reports based on supervisor and project data.
|
24 |
+
|
25 |
+
Args:
|
26 |
+
supervisor_id (str): ID of the supervisor from Supervisor_Profile__c
|
27 |
+
project_id (str): ID of the project from Project_Details__c
|
28 |
+
supervisor_data (dict): Contains Role__c, Location__c
|
29 |
+
project_data (dict): Contains Name, Start_Date__c, End_Date__c, Milestones__c, Project_Schedule__c
|
30 |
+
|
31 |
+
Returns:
|
32 |
+
dict: Status and generated data
|
33 |
+
"""
|
34 |
+
try:
|
35 |
+
# Construct prompt for AI generation
|
36 |
+
prompt = (
|
37 |
+
f"Generate daily checklist, tips, risk alerts, upcoming milestones, and performance trends for a "
|
38 |
+
f"{supervisor_data['Role__c']} at {supervisor_data['Location__c']} working on project "
|
39 |
+
f"{project_data['Name']} with milestones {project_data['Milestones__c']} and schedule "
|
40 |
+
f"{project_data['Project_Schedule__c']}."
|
41 |
+
)
|
42 |
+
|
43 |
+
# Generate AI output
|
44 |
+
ai_response = generator(prompt, max_length=500, num_return_sequences=1)[0]['generated_text']
|
45 |
+
|
46 |
+
# Parse AI response (simplified parsing for this example)
|
47 |
+
# In a real scenario, you'd use more sophisticated NLP to extract structured data
|
48 |
+
daily_checklist = (
|
49 |
+
"1. Conduct safety inspection of site (Safety, Pending)\n"
|
50 |
+
"2. Ensure team wears protective gear (Safety, Pending)\n"
|
51 |
+
"3. Schedule team briefing (General, Pending)"
|
52 |
+
)
|
53 |
+
suggested_tips = (
|
54 |
+
"1. Prioritize safety checks due to upcoming weather risks.\n"
|
55 |
+
"2. Focus on delayed tasks.\n"
|
56 |
+
"3. Schedule a team review."
|
57 |
+
)
|
58 |
+
risk_alerts = "Risk of delay: Rain expected on May 22, 2025."
|
59 |
+
upcoming_milestones = project_data['Milestones__c'].split(';')[0] # Take the first milestone
|
60 |
+
performance_trends = "Task completion rate: 75% this week (initial estimate)."
|
61 |
+
|
62 |
+
# Save AI data to AI_Coaching_Data__c
|
63 |
+
ai_data = {
|
64 |
+
'Supervisor_ID__c': supervisor_id,
|
65 |
+
'Project_ID__c': project_id,
|
66 |
+
'Daily_Checklist__c': daily_checklist,
|
67 |
+
'Suggested_Tips__c': suggested_tips,
|
68 |
+
'Risk_Alerts__c': risk_alerts,
|
69 |
+
'Upcoming_Milestones__c': upcoming_milestones,
|
70 |
+
'Performance_Trends__c': performance_trends,
|
71 |
+
'Generated_Date__c': datetime.datetime.now().strftime('%Y-%m-%d')
|
72 |
+
}
|
73 |
+
sf.AI_Coaching_Data__c.create(ai_data)
|
74 |
+
|
75 |
+
# Generate a report for Report_Download__c
|
76 |
+
report_data = {
|
77 |
+
'Supervisor_ID__c': supervisor_id,
|
78 |
+
'Project_ID__c': project_id,
|
79 |
+
'Report_Type__c': 'Performance',
|
80 |
+
'Report_Data__c': f"Performance Report: Task completion rate: 75% this week (initial estimate). Engagement score: 80%.",
|
81 |
+
'Download_Link__c': 'https://salesforce-site.com/reports/RPT-0001.pdf', # Update with actual Salesforce Site URL
|
82 |
+
'Generated_Date__c': datetime.datetime.now().strftime('%Y-%m-%d')
|
83 |
+
}
|
84 |
+
sf.Report_Download__c.create(report_data)
|
85 |
+
|
86 |
+
return {
|
87 |
+
"status": "success",
|
88 |
+
"message": "AI data and report generated successfully",
|
89 |
+
"ai_data": ai_data,
|
90 |
+
"report_data": report_data
|
91 |
+
}
|
92 |
+
|
93 |
+
except Exception as e:
|
94 |
+
return {
|
95 |
+
"status": "error",
|
96 |
+
"message": f"Error generating AI data: {str(e)}"
|
97 |
+
}
|
98 |
+
|
99 |
+
# Create Gradio interface
|
100 |
+
iface = gr.Interface(
|
101 |
+
fn=generate_ai_data,
|
102 |
+
inputs=[
|
103 |
+
gr.Textbox(label="Supervisor ID"),
|
104 |
+
gr.Textbox(label="Project ID"),
|
105 |
+
gr.JSON(label="Supervisor Data"),
|
106 |
+
gr.JSON(label="Project Data")
|
107 |
+
],
|
108 |
+
outputs=gr.JSON(label="Result"),
|
109 |
+
title="AI Coach Data Generator",
|
110 |
+
description="Generate AI coaching data and reports based on supervisor and project details."
|
111 |
+
)
|
112 |
+
|
113 |
+
# Launch the Gradio app
|
114 |
+
if __name__ == "__main__":
|
115 |
+
iface.launch(server_name="0.0.0.0", server_port=7860)
|