Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import logging
|
| 3 |
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
|
@@ -37,7 +38,6 @@ MODEL_PATH = "your_actual_username/fine_tuned_bart_construction" # Replace with
|
|
| 37 |
FALLBACK_MODEL = "facebook/bart-large" # Fallback public model
|
| 38 |
|
| 39 |
try:
|
| 40 |
-
# Try loading fine-tuned model
|
| 41 |
logging.info(f"Attempting to load model from {MODEL_PATH}")
|
| 42 |
model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_PATH, use_auth_token=HF_TOKEN)
|
| 43 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH, use_auth_token=HF_TOKEN)
|
|
@@ -59,7 +59,7 @@ class ChecklistInput(BaseModel):
|
|
| 59 |
project_id: str = "Unknown"
|
| 60 |
project_name: str = "Unknown Project"
|
| 61 |
milestones: str = "No milestones provided"
|
| 62 |
-
record_id: str = None #
|
| 63 |
supervisor_id: str = None # Supervisor_ID__c
|
| 64 |
project_id_sf: str = None # Project_ID__c
|
| 65 |
coaching_id: str = None # Coaching_ID__c
|
|
@@ -70,31 +70,31 @@ app = FastAPI()
|
|
| 70 |
@app.post("/generate")
|
| 71 |
async def generate_checklist(data: ChecklistInput):
|
| 72 |
try:
|
| 73 |
-
#
|
| 74 |
inputs = f"Role: {data.role} Project: {data.project_id} ({data.project_name}) Milestones: {data.milestones}"
|
| 75 |
logging.info(f"Generating checklist for inputs: {inputs[:100]}...")
|
| 76 |
-
|
| 77 |
-
# Tokenize and generate
|
| 78 |
input_ids = tokenizer(inputs, return_tensors="pt", max_length=128, truncation=True).input_ids
|
| 79 |
outputs = model.generate(input_ids, max_length=128, num_beams=4, early_stopping=True)
|
| 80 |
checklist = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 81 |
-
|
| 82 |
-
# Static tips (replace with model-generated tips after fine-tuning)
|
| 83 |
tips = "1. Prioritize safety checks\n2. Review milestones\n3. Log progress"
|
| 84 |
-
|
| 85 |
-
# Basic KPI logic: Flag if milestones contain "delay" or similar
|
| 86 |
kpi_flag = "delay" in data.milestones.lower() or "behind" in data.milestones.lower()
|
| 87 |
|
| 88 |
-
# Update Salesforce
|
| 89 |
if data.record_id:
|
| 90 |
try:
|
| 91 |
sf = get_salesforce_connection()
|
| 92 |
-
|
| 93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
update_data = {
|
| 95 |
'Daily_Checklist__c': checklist,
|
| 96 |
'Suggested_Tips__c': tips,
|
| 97 |
-
'Engagement_Score__c':
|
| 98 |
'KPI_Flag__c': kpi_flag,
|
| 99 |
'Supervisor_ID__c': data.supervisor_id if data.supervisor_id else existing_record.get('Supervisor_ID__c'),
|
| 100 |
'Project_ID__c': data.project_id_sf if data.project_id_sf else existing_record.get('Project_ID__c'),
|
|
@@ -103,7 +103,7 @@ async def generate_checklist(data: ChecklistInput):
|
|
| 103 |
'Download_Link__c': existing_record.get('Download_Link__c', '')
|
| 104 |
}
|
| 105 |
sf.Supervisor_AI_Coaching__c.update(data.record_id, update_data)
|
| 106 |
-
logging.info(f"Updated Salesforce record {data.record_id}")
|
| 107 |
except Exception as sf_e:
|
| 108 |
logging.error(f"Failed to update Salesforce: {sf_e}")
|
| 109 |
|
|
@@ -127,15 +127,21 @@ def gradio_generate_checklist(role, project_id, project_name, milestones, record
|
|
| 127 |
tips = "1. Prioritize safety checks\n2. Review milestones\n3. Log progress"
|
| 128 |
kpi_flag = "delay" in milestones.lower() or "behind" in milestones.lower()
|
| 129 |
|
| 130 |
-
# Update Salesforce if record_id is provided
|
| 131 |
status = "No Salesforce update (record_id not provided)"
|
| 132 |
if record_id:
|
| 133 |
sf = get_salesforce_connection()
|
| 134 |
-
existing_record = sf.Supervisor_AI_Coaching__c.get(record_id, default=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
update_data = {
|
| 136 |
'Daily_Checklist__c': checklist,
|
| 137 |
'Suggested_Tips__c': tips,
|
| 138 |
-
'Engagement_Score__c':
|
| 139 |
'KPI_Flag__c': kpi_flag,
|
| 140 |
'Supervisor_ID__c': supervisor_id if supervisor_id else existing_record.get('Supervisor_ID__c'),
|
| 141 |
'Project_ID__c': project_id_sf if project_id_sf else existing_record.get('Project_ID__c'),
|
|
@@ -144,7 +150,7 @@ def gradio_generate_checklist(role, project_id, project_name, milestones, record
|
|
| 144 |
'Download_Link__c': existing_record.get('Download_Link__c', '')
|
| 145 |
}
|
| 146 |
sf.Supervisor_AI_Coaching__c.update(record_id, update_data)
|
| 147 |
-
status = f"Updated Salesforce record {record_id}"
|
| 148 |
|
| 149 |
return checklist, tips, kpi_flag, status
|
| 150 |
except Exception as e:
|
|
@@ -159,8 +165,8 @@ iface = gr.Interface(
|
|
| 159 |
gr.Textbox(label="Project Name", value="Building A"),
|
| 160 |
gr.Textbox(label="Milestones", value="Complete foundation by 5/15"),
|
| 161 |
gr.Textbox(label="Salesforce Record ID (optional)", value=""),
|
| 162 |
-
gr.Textbox(label="Supervisor ID (optional)", value=""),
|
| 163 |
-
gr.Textbox(label="Project ID (Salesforce, optional)", value=""),
|
| 164 |
gr.Textbox(label="Coaching ID (optional)", value="")
|
| 165 |
],
|
| 166 |
outputs=[
|
|
@@ -182,4 +188,5 @@ if __name__ == "__main__":
|
|
| 182 |
logging.info("Gradio interface launched successfully")
|
| 183 |
except Exception as e:
|
| 184 |
logging.error(f"Failed to launch Gradio interface: {e}")
|
| 185 |
-
raise e
|
|
|
|
|
|
| 1 |
+
```python
|
| 2 |
import gradio as gr
|
| 3 |
import logging
|
| 4 |
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
|
|
|
| 38 |
FALLBACK_MODEL = "facebook/bart-large" # Fallback public model
|
| 39 |
|
| 40 |
try:
|
|
|
|
| 41 |
logging.info(f"Attempting to load model from {MODEL_PATH}")
|
| 42 |
model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_PATH, use_auth_token=HF_TOKEN)
|
| 43 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH, use_auth_token=HF_TOKEN)
|
|
|
|
| 59 |
project_id: str = "Unknown"
|
| 60 |
project_name: str = "Unknown Project"
|
| 61 |
milestones: str = "No milestones provided"
|
| 62 |
+
record_id: str = None # Supervisor_AI_Coaching__c record ID
|
| 63 |
supervisor_id: str = None # Supervisor_ID__c
|
| 64 |
project_id_sf: str = None # Project_ID__c
|
| 65 |
coaching_id: str = None # Coaching_ID__c
|
|
|
|
| 70 |
@app.post("/generate")
|
| 71 |
async def generate_checklist(data: ChecklistInput):
|
| 72 |
try:
|
| 73 |
+
# Generate checklist and tips
|
| 74 |
inputs = f"Role: {data.role} Project: {data.project_id} ({data.project_name}) Milestones: {data.milestones}"
|
| 75 |
logging.info(f"Generating checklist for inputs: {inputs[:100]}...")
|
|
|
|
|
|
|
| 76 |
input_ids = tokenizer(inputs, return_tensors="pt", max_length=128, truncation=True).input_ids
|
| 77 |
outputs = model.generate(input_ids, max_length=128, num_beams=4, early_stopping=True)
|
| 78 |
checklist = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
|
|
|
|
|
|
| 79 |
tips = "1. Prioritize safety checks\n2. Review milestones\n3. Log progress"
|
|
|
|
|
|
|
| 80 |
kpi_flag = "delay" in data.milestones.lower() or "behind" in data.milestones.lower()
|
| 81 |
|
| 82 |
+
# Update Salesforce if record_id is provided
|
| 83 |
if data.record_id:
|
| 84 |
try:
|
| 85 |
sf = get_salesforce_connection()
|
| 86 |
+
existing_record = sf.Supervisor_AI_Coaching__c.get(data.record_id, default={
|
| 87 |
+
'Supervisor_ID__c': None,
|
| 88 |
+
'Project_ID__c': None,
|
| 89 |
+
'Coaching_ID__c': None,
|
| 90 |
+
'Reflection_Log__c': '',
|
| 91 |
+
'Download_Link__c': '',
|
| 92 |
+
'Engagement_Score__c': 0
|
| 93 |
+
})
|
| 94 |
update_data = {
|
| 95 |
'Daily_Checklist__c': checklist,
|
| 96 |
'Suggested_Tips__c': tips,
|
| 97 |
+
'Engagement_Score__c': existing_record.get('Engagement_Score__c', 0) + 10,
|
| 98 |
'KPI_Flag__c': kpi_flag,
|
| 99 |
'Supervisor_ID__c': data.supervisor_id if data.supervisor_id else existing_record.get('Supervisor_ID__c'),
|
| 100 |
'Project_ID__c': data.project_id_sf if data.project_id_sf else existing_record.get('Project_ID__c'),
|
|
|
|
| 103 |
'Download_Link__c': existing_record.get('Download_Link__c', '')
|
| 104 |
}
|
| 105 |
sf.Supervisor_AI_Coaching__c.update(data.record_id, update_data)
|
| 106 |
+
logging.info(f"Updated Salesforce record {data.record_id} with fields: {update_data}")
|
| 107 |
except Exception as sf_e:
|
| 108 |
logging.error(f"Failed to update Salesforce: {sf_e}")
|
| 109 |
|
|
|
|
| 127 |
tips = "1. Prioritize safety checks\n2. Review milestones\n3. Log progress"
|
| 128 |
kpi_flag = "delay" in milestones.lower() or "behind" in milestones.lower()
|
| 129 |
|
|
|
|
| 130 |
status = "No Salesforce update (record_id not provided)"
|
| 131 |
if record_id:
|
| 132 |
sf = get_salesforce_connection()
|
| 133 |
+
existing_record = sf.Supervisor_AI_Coaching__c.get(record_id, default={
|
| 134 |
+
'Supervisor_ID__c': None,
|
| 135 |
+
'Project_ID__c': None,
|
| 136 |
+
'Coaching_ID__c': None,
|
| 137 |
+
'Reflection_Log__c': '',
|
| 138 |
+
'Download_Link__c': '',
|
| 139 |
+
'Engagement_Score__c': 0
|
| 140 |
+
})
|
| 141 |
update_data = {
|
| 142 |
'Daily_Checklist__c': checklist,
|
| 143 |
'Suggested_Tips__c': tips,
|
| 144 |
+
'Engagement_Score__c': existing_record.get('Engagement_Score__c', 0) + 10,
|
| 145 |
'KPI_Flag__c': kpi_flag,
|
| 146 |
'Supervisor_ID__c': supervisor_id if supervisor_id else existing_record.get('Supervisor_ID__c'),
|
| 147 |
'Project_ID__c': project_id_sf if project_id_sf else existing_record.get('Project_ID__c'),
|
|
|
|
| 150 |
'Download_Link__c': existing_record.get('Download_Link__c', '')
|
| 151 |
}
|
| 152 |
sf.Supervisor_AI_Coaching__c.update(record_id, update_data)
|
| 153 |
+
status = f"Updated Salesforce record {record_id} with fields: {update_data}"
|
| 154 |
|
| 155 |
return checklist, tips, kpi_flag, status
|
| 156 |
except Exception as e:
|
|
|
|
| 165 |
gr.Textbox(label="Project Name", value="Building A"),
|
| 166 |
gr.Textbox(label="Milestones", value="Complete foundation by 5/15"),
|
| 167 |
gr.Textbox(label="Salesforce Record ID (optional)", value=""),
|
| 168 |
+
gr.Textbox(label="Supervisor ID (Salesforce User ID, optional)", value=""),
|
| 169 |
+
gr.Textbox(label="Project ID (Salesforce Project__c ID, optional)", value=""),
|
| 170 |
gr.Textbox(label="Coaching ID (optional)", value="")
|
| 171 |
],
|
| 172 |
outputs=[
|
|
|
|
| 188 |
logging.info("Gradio interface launched successfully")
|
| 189 |
except Exception as e:
|
| 190 |
logging.error(f"Failed to launch Gradio interface: {e}")
|
| 191 |
+
raise e
|
| 192 |
+
```
|