yetessam commited on
Commit
5d06296
·
verified ·
1 Parent(s): 032d77d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -42
app.py CHANGED
@@ -5,6 +5,7 @@ import datetime
5
  import requests
6
  from checks.failed_check import create_failed_gradio_ui
7
  from checks.endpoint_check import check_public_endpoint
 
8
 
9
  import os
10
  import pytz # Had to give it permission in Code agent
@@ -15,65 +16,66 @@ from agents.model import load_huggingface_model
15
  from agents.prompts import load_prompts
16
 
17
  from ContentGradio import ContentAgentUI
 
 
 
 
 
 
 
 
 
18
 
 
19
 
20
- # Prechecks
21
-
22
- # Get the URI for the endpoint
23
- endpoint_uri = load_huggingface_model()
24
-
25
- # Test the endpoint
26
-
27
- status_info = check_public_endpoint(endpoint_uri)
28
 
29
-
30
- print(status_info["status"])
31
- print(status_info["status_code"])
32
-
33
 
34
- if status_info["status"] is False or status_info["status_code"] == "503":
35
- interface = create_failed_gradio_ui(status_info)
36
- # Launch the UI
37
- interface.launch(show_error=True)
38
- else:
39
 
 
40
  model = HfApiModel(
41
  max_tokens=2096,
42
  temperature=0.5,
43
- model_id= endpoint_uri,
44
  custom_role_conversions=None,
45
- )
46
-
47
- # Load prompts
48
- combined_prompts = load_prompts()
49
- combined_tools = load_tools() ## from the tools folder (don't remove final answer)
50
-
51
- # Try a direct test of your endpoint
52
- test_prompt = "Hello, how are you?"
53
- response = requests.post(
54
- endpoint_uri,
55
- json={"inputs": test_prompt},
56
- headers={"Authorization": f"Bearer {os.getenv('HF_TOKEN')}"}
57
  )
58
- print(response.json())
59
-
60
- agent = CodeAgent(
61
  model=model,
62
- tools=combined_tools,
63
  max_steps=6,
64
  verbosity_level=3,
65
- grammar=None,
66
- planning_interval=None,
67
  name="content_agent",
68
- description="Evaluates whether text is polite or impolite. ",
69
- prompt_templates=combined_prompts,
70
  additional_authorized_imports=["pytz"]
71
-
72
- )
 
 
 
73
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
  # Create an instance of the ContentAgentUI class
75
  ui = ContentAgentUI()
 
76
 
77
- # Pass through the agent
78
- ui.pass_through_agent(agent)
79
 
 
 
 
5
  import requests
6
  from checks.failed_check import create_failed_gradio_ui
7
  from checks.endpoint_check import check_public_endpoint
8
+ from checks.health_check import check_model_endpoint, should_launch_ui
9
 
10
  import os
11
  import pytz # Had to give it permission in Code agent
 
16
  from agents.prompts import load_prompts
17
 
18
  from ContentGradio import ContentAgentUI
19
+
20
+ # Try a direct test of your endpoint
21
+ test_prompt = "Hello, how are you?"
22
+ response = requests.post(
23
+ endpoint_uri,
24
+ json={"inputs": test_prompt},
25
+ headers={"Authorization": f"Bearer {os.getenv('HF_TOKEN')}"}
26
+ )
27
+ print(response.json())
28
 
29
+
30
 
31
+
32
+ def initialize_agent(endpoint_uri: str):
 
 
 
 
 
 
33
 
34
+ # Load prompts
35
+ combined_prompts = load_prompts()
36
+ combined_tools = load_tools() ## from the tools folder (don't remove final answer)
 
37
 
 
 
 
 
 
38
 
39
+ """Initialize and configure the CodeAgent"""
40
  model = HfApiModel(
41
  max_tokens=2096,
42
  temperature=0.5,
43
+ model_id=endpoint_uri,
44
  custom_role_conversions=None,
 
 
 
 
 
 
 
 
 
 
 
 
45
  )
46
+
47
+ return CodeAgent(
 
48
  model=model,
49
+ tools=combined_tools(),
50
  max_steps=6,
51
  verbosity_level=3,
 
 
52
  name="content_agent",
53
+ description="Evaluates whether text is polite or impolite.",
54
+ prompt_templates=combined_prompts(),
55
  additional_authorized_imports=["pytz"]
56
+ )
57
+
58
+
59
+ def main():
60
+ # Load endpoint and check health
61
 
62
+
63
+
64
+ # Prechecks
65
+ endpoint_uri = load_huggingface_model() # Get the URI for the endpoint
66
+ is_healthy, status_info = check_model_endpoint(endpoint_uri) # Test the endpoint
67
+
68
+ if not is_healthy:
69
+ interface = create_failed_gradio_ui(status_info)
70
+ interface.launch(show_error=True)
71
+ return
72
+
73
+ # Initialize and run the agent
74
+ agent = initialize_agent(endpoint_uri)
75
  # Create an instance of the ContentAgentUI class
76
  ui = ContentAgentUI()
77
+ ui.pass_through_agent(agent) # Pass through the agent
78
 
 
 
79
 
80
+ if __name__ == "__main__":
81
+ main()