lokeshloki143 commited on
Commit
a9c787b
·
verified ·
1 Parent(s): e8067ae

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -46
app.py CHANGED
@@ -1,4 +1,3 @@
1
-
2
  import gradio as gr
3
  from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
4
  import os
@@ -12,12 +11,27 @@ load_dotenv()
12
 
13
  # Salesforce connection
14
  def get_salesforce_connection():
15
- sf = Salesforce(
16
- username=os.getenv("onteddugeetha104@gmail.com"),
17
- password=os.getenv("9032590825g@"),
18
- security_token=os.getenv("tTPLQduw8wDpxdKOMJ3d9dM3o")
19
- )
20
- return sf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
  # Load Hugging Face token
23
  HF_TOKEN = os.getenv("HF_TOKEN")
@@ -26,8 +40,11 @@ HF_TOKEN = os.getenv("HF_TOKEN")
26
  MODEL_PATH = "facebook/bart-large" # Public model
27
  # MODEL_PATH = "your_actual_username/fine_tuned_bart_construction" # Uncomment after uploading
28
 
29
- model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_PATH, use_auth_token=HF_TOKEN if HF_TOKEN else None)
30
- tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH, use_auth_token=HF_TOKEN if HF_TOKEN else None)
 
 
 
31
 
32
  # Define input model for FastAPI
33
  class ChecklistInput(BaseModel):
@@ -46,43 +63,46 @@ app = FastAPI()
46
 
47
  @app.post("/generate")
48
  async def generate_checklist(data: ChecklistInput):
49
- inputs = f"Role: {data.role} Project: {data.project_id} ({data.project_name}) Milestones: {data.milestones}"
50
- input_ids = tokenizer(inputs, return_tensors="pt", max_length=128, truncation=True).input_ids
51
- outputs = model.generate(input_ids, max_length=128, num_beams=4, early_stopping=True)
52
- checklist = tokenizer.decode(outputs[0], skip_special_tokens=True)
53
- tips = "1. Prioritize safety checks\n2. Review milestones\n3. Log progress"
54
- kpi_flag = "delay" in data.milestones.lower() or "behind" in data.milestones.lower()
55
-
56
- if data.record_id:
57
- sf = get_salesforce_connection()
58
- existing_record = sf.Supervisor_AI_Coaching__c.get(data.record_id, default={
59
- 'Name': '',
60
- 'Supervisor_ID__c': None,
61
- 'Project_ID__c': None,
62
- 'Reflection_Log__c': '',
63
- 'Download_Link__c': '',
64
- 'Engagement_Score__c': 0,
65
- 'KPI_Flag__c': False,
66
- 'Daily_Checklist__c': '',
67
- 'Suggested_Tips__c': ''
68
- })
69
- update_data = {
70
- 'Daily_Checklist__c': checklist,
71
- 'Suggested_Tips__c': tips,
72
- 'Engagement_Score__c': existing_record.get('Engagement_Score__c', 0) + 10,
73
- 'KPI_Flag__c': kpi_flag,
74
- 'Supervisor_ID__c': data.supervisor_id if data.supervisor_id else existing_record.get('Supervisor_ID__c'),
75
- 'Project_ID__c': data.project_id_sf if data.project_id_sf else existing_record.get('Project_ID__c'),
76
- 'Reflection_Log__c': data.reflection_log if data.reflection_log else existing_record.get('Reflection_Log__c', ''),
77
- 'Download_Link__c': data.download_link if data.download_link else existing_record.get('Download_Link__c', '')
 
 
 
 
 
 
 
 
78
  }
79
- sf.Supervisor_AI_Coaching__c.update(data.record_id, update_data)
80
-
81
- return {
82
- "checklist": checklist,
83
- "tips": tips,
84
- "kpi_flag": kpi_flag
85
- }
86
 
87
  # Login and display records
88
  def login_and_display(project_id_sf):
@@ -191,4 +211,7 @@ with gr.Blocks() as iface:
191
  iface.app = app
192
 
193
  if __name__ == "__main__":
194
- iface.launch(server_name="0.0.0.0", server_port=7860, share=False)
 
 
 
 
 
1
  import gradio as gr
2
  from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
3
  import os
 
11
 
12
  # Salesforce connection
13
  def get_salesforce_connection():
14
+ try:
15
+ username = os.getenv("SF_USERNAME")
16
+ password = os.getenv("SF_PASSWORD")
17
+ security_token = os.getenv("SF_SECURITY_TOKEN")
18
+ domain = os.getenv("SF_DOMAIN", "login") # Default to production (login.salesforce.com)
19
+
20
+ if not all([username, password, security_token]):
21
+ raise ValueError("Missing Salesforce credentials. Check SF_USERNAME, SF_PASSWORD, SF_SECURITY_TOKEN in .env")
22
+
23
+ sf = Salesforce(
24
+ username=username,
25
+ password=password,
26
+ security_token=security_token,
27
+ domain=domain
28
+ )
29
+
30
+ # Test connection by fetching user info
31
+ sf.User.get(sf.user_id)
32
+ return sf
33
+ except Exception as e:
34
+ raise Exception(f"Failed to connect to Salesforce: {str(e)}")
35
 
36
  # Load Hugging Face token
37
  HF_TOKEN = os.getenv("HF_TOKEN")
 
40
  MODEL_PATH = "facebook/bart-large" # Public model
41
  # MODEL_PATH = "your_actual_username/fine_tuned_bart_construction" # Uncomment after uploading
42
 
43
+ try:
44
+ model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_PATH, use_auth_token=HF_TOKEN if HF_TOKEN else None)
45
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH, use_auth_token=HF_TOKEN if HF_TOKEN else None)
46
+ except Exception as e:
47
+ raise Exception(f"Failed to load model: {str(e)}")
48
 
49
  # Define input model for FastAPI
50
  class ChecklistInput(BaseModel):
 
63
 
64
  @app.post("/generate")
65
  async def generate_checklist(data: ChecklistInput):
66
+ try:
67
+ inputs = f"Role: {data.role} Project: {data.project_id} ({data.project_name}) Milestones: {data.milestones}"
68
+ input_ids = tokenizer(inputs, return_tensors="pt", max_length=128, truncation=True).input_ids
69
+ outputs = model.generate(input_ids, max_length=128, num_beams=4, early_stopping=True)
70
+ checklist = tokenizer.decode(outputs[0], skip_special_tokens=True)
71
+ tips = "1. Prioritize safety checks\n2. Review milestones\n3. Log progress"
72
+ kpi_flag = "delay" in data.milestones.lower() or "behind" in data.milestones.lower()
73
+
74
+ if data.record_id:
75
+ sf = get_salesforce_connection()
76
+ existing_record = sf.Supervisor_AI_Coaching__c.get(data.record_id, default={
77
+ 'Name': '',
78
+ 'Supervisor_ID__c': None,
79
+ 'Project_ID__c': None,
80
+ 'Reflection_Log__c': '',
81
+ 'Download_Link__c': '',
82
+ 'Engagement_Score__c': 0,
83
+ 'KPI_Flag__c': False,
84
+ 'Daily_Checklist__c': '',
85
+ 'Suggested_Tips__c': ''
86
+ })
87
+ update_data = {
88
+ 'Daily_Checklist__c': checklist,
89
+ 'Suggested_Tips__c': tips,
90
+ 'Engagement_Score__c': existing_record.get('Engagement_Score__c', 0) + 10,
91
+ 'KPI_Flag__c': kpi_flag,
92
+ 'Supervisor_ID__c': data.supervisor_id if data.supervisor_id else existing_record.get('Supervisor_ID__c'),
93
+ 'Project_ID__c': data.project_id_sf if data.project_id_sf else existing_record.get('Project_ID__c'),
94
+ 'Reflection_Log__c': data.reflection_log if data.reflection_log else existing_record.get('Reflection_Log__c', ''),
95
+ 'Download_Link__c': data.download_link if data.download_link else existing_record.get('Download_Link__c', '')
96
+ }
97
+ sf.Supervisor_AI_Coaching__c.update(data.record_id, update_data)
98
+
99
+ return {
100
+ "checklist": checklist,
101
+ "tips": tips,
102
+ "kpi_flag": kpi_flag
103
  }
104
+ except Exception as e:
105
+ return {"error": str(e)}
 
 
 
 
 
106
 
107
  # Login and display records
108
  def login_and_display(project_id_sf):
 
211
  iface.app = app
212
 
213
  if __name__ == "__main__":
214
+ try:
215
+ iface.launch(server_name="0.0.0.0", server_port=7860, share=False)
216
+ except Exception as e:
217
+ print(f"Failed to launch Gradio: {str(e)}")