Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,34 +4,19 @@ from simple_salesforce import Salesforce
|
|
4 |
import datetime
|
5 |
import os
|
6 |
from dotenv import load_dotenv
|
7 |
-
import logging
|
8 |
-
|
9 |
-
# Configure logging for better debugging
|
10 |
-
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
11 |
-
logger = logging.getLogger(__name__)
|
12 |
|
13 |
# Load environment variables from .env file
|
14 |
load_dotenv()
|
15 |
|
16 |
# Initialize Hugging Face model
|
17 |
-
|
18 |
-
logger.info("Initializing distilgpt2 model...")
|
19 |
-
generator = pipeline("text-generation", model="distilgpt2")
|
20 |
-
except Exception as e:
|
21 |
-
logger.error(f"Failed to initialize model: {str(e)}")
|
22 |
-
raise RuntimeError(f"Model initialization failed: {str(e)}. Ensure PyTorch or TensorFlow is installed.")
|
23 |
|
24 |
# Initialize Salesforce connection using environment variables
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
security_token=os.getenv("SF_SECURITY_TOKEN")
|
31 |
-
)
|
32 |
-
except Exception as e:
|
33 |
-
logger.error(f"Salesforce connection failed: {str(e)}")
|
34 |
-
raise RuntimeError(f"Salesforce connection failed: {str(e)}")
|
35 |
|
36 |
def generate_ai_data(supervisor_id, project_id, supervisor_data, project_data):
|
37 |
"""
|
@@ -56,10 +41,10 @@ def generate_ai_data(supervisor_id, project_id, supervisor_data, project_data):
|
|
56 |
)
|
57 |
|
58 |
# Generate AI output
|
59 |
-
logger.info("Generating AI response...")
|
60 |
ai_response = generator(prompt, max_length=500, num_return_sequences=1)[0]['generated_text']
|
61 |
|
62 |
# Parse AI response (simplified parsing for this example)
|
|
|
63 |
daily_checklist = (
|
64 |
"1. Conduct safety inspection of site (Safety, Pending)\n"
|
65 |
"2. Ensure team wears protective gear (Safety, Pending)\n"
|
@@ -75,7 +60,6 @@ def generate_ai_data(supervisor_id, project_id, supervisor_data, project_data):
|
|
75 |
performance_trends = "Task completion rate: 75% this week (initial estimate)."
|
76 |
|
77 |
# Save AI data to AI_Coaching_Data__c
|
78 |
-
logger.info("Saving AI data to Salesforce...")
|
79 |
ai_data = {
|
80 |
'Supervisor_ID__c': supervisor_id,
|
81 |
'Project_ID__c': project_id,
|
@@ -89,7 +73,6 @@ def generate_ai_data(supervisor_id, project_id, supervisor_data, project_data):
|
|
89 |
sf.AI_Coaching_Data__c.create(ai_data)
|
90 |
|
91 |
# Generate a report for Report_Download__c
|
92 |
-
logger.info("Generating report for Salesforce...")
|
93 |
report_data = {
|
94 |
'Supervisor_ID__c': supervisor_id,
|
95 |
'Project_ID__c': project_id,
|
@@ -100,7 +83,6 @@ def generate_ai_data(supervisor_id, project_id, supervisor_data, project_data):
|
|
100 |
}
|
101 |
sf.Report_Download__c.create(report_data)
|
102 |
|
103 |
-
logger.info("AI data and report generated successfully")
|
104 |
return {
|
105 |
"status": "success",
|
106 |
"message": "AI data and report generated successfully",
|
@@ -109,14 +91,12 @@ def generate_ai_data(supervisor_id, project_id, supervisor_data, project_data):
|
|
109 |
}
|
110 |
|
111 |
except Exception as e:
|
112 |
-
logger.error(f"Error generating AI data: {str(e)}")
|
113 |
return {
|
114 |
"status": "error",
|
115 |
"message": f"Error generating AI data: {str(e)}"
|
116 |
}
|
117 |
|
118 |
# Create Gradio interface
|
119 |
-
logger.info("Setting up Gradio interface...")
|
120 |
iface = gr.Interface(
|
121 |
fn=generate_ai_data,
|
122 |
inputs=[
|
@@ -132,5 +112,4 @@ iface = gr.Interface(
|
|
132 |
|
133 |
# Launch the Gradio app
|
134 |
if __name__ == "__main__":
|
135 |
-
logger.info("Launching Gradio app...")
|
136 |
iface.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
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 |
"""
|
|
|
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"
|
|
|
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,
|
|
|
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,
|
|
|
83 |
}
|
84 |
sf.Report_Download__c.create(report_data)
|
85 |
|
|
|
86 |
return {
|
87 |
"status": "success",
|
88 |
"message": "AI data and report generated successfully",
|
|
|
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=[
|
|
|
112 |
|
113 |
# Launch the Gradio app
|
114 |
if __name__ == "__main__":
|
|
|
115 |
iface.launch(server_name="0.0.0.0", server_port=7860)
|