aslan-ng commited on
Commit
7dd6cdd
·
verified ·
1 Parent(s): f1ee4f2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -33
app.py CHANGED
@@ -1,3 +1,5 @@
 
 
1
  import os
2
  import json
3
  import smolagents
@@ -10,8 +12,8 @@ import difflib
10
  import openai
11
  from langchain_community.utilities.wikipedia import WikipediaAPIWrapper
12
  import gradio as gr
13
- print("smolagents version:", smolagents.__version__)
14
- print("openai version:", openai.__version__)
15
 
16
  REPO_ID_TECHSPARK_STAFF = "aslan-ng/CMU_TechSpark_Staff"
17
  REPO_ID_TECHSPARK_COURSES = "aslan-ng/CMU_TechSpark_Courses"
@@ -24,6 +26,8 @@ login(hf_token)
24
 
25
  NUMERIC_PROFILE = ["Laser Cutting", "Wood Working", "Wood CNC", "Metal Machining", "Metal CNC", "3D Printer", "Welding", "Electronics"]
26
 
 
 
27
  def load_data_from_huggingface():
28
  """
29
  Loads data from HuggingFace.
@@ -52,6 +56,8 @@ def load_data_from_huggingface():
52
 
53
  staff_df, courses_df, tools_df, nodes_df, edges_df = load_data_from_huggingface()
54
 
 
 
55
  def vector_1st_distance(x: list, y: list):
56
  """
57
  Calculate the average 1st distance between two vectors.
@@ -102,6 +108,8 @@ def skill_score(
102
  y.append(electronics)
103
  return vector_1st_distance(x, y)
104
 
 
 
105
  def all_staff():
106
  """
107
  Return a list of all staff.
@@ -246,6 +254,8 @@ class FindSuitableStaff(smolagents.tools.Tool):
246
  staff_profiles = [get_staff_profile(name) for name in names]
247
  return json.dumps(staff_profiles)
248
 
 
 
249
  def all_courses_code():
250
  """
251
  Return a list of all course codes.
@@ -406,6 +416,8 @@ class FindSuitableCourses(smolagents.tools.Tool):
406
  options = [get_course_profile(course) for course in matches]
407
  return json.dumps(options)
408
 
 
 
409
  def all_tools():
410
  """
411
  Return a list of all tools and machines.
@@ -488,6 +500,8 @@ class CheckMachineAccessibility(smolagents.tools.Tool):
488
  def forward(self, name: str) -> str:
489
  return json.dumps(is_tool_accessible(name))
490
 
 
 
491
  class WikipediaSearch(smolagents.Tool):
492
  """
493
  Create tool for searching Wikipedia
@@ -506,6 +520,8 @@ class WikipediaSearch(smolagents.Tool):
506
  answer = wikipedia_api.run(query)
507
  return answer
508
 
 
 
509
  def all_nodes():
510
  """
511
  Return a list of all nodes name.
@@ -553,36 +569,6 @@ def load_graph(nodes_df, edges_df):
553
 
554
  G = load_graph(nodes_df, edges_df)
555
 
556
- def show_graph():
557
- import matplotlib.pyplot as plt
558
-
559
- pos = {
560
- row["ID"]: (row["X"], row["Y"])
561
- for _, row in nodes_df.iterrows()
562
- }
563
-
564
- labels = {
565
- row["ID"]: row["Name"]
566
- for _, row in nodes_df.iterrows()
567
- }
568
-
569
- plt.figure(figsize=(8, 6))
570
-
571
- nx.draw(
572
- G,
573
- pos=pos,
574
- #labels=labels, # ID or Name?
575
- with_labels=True,
576
- node_size=800,
577
- node_color="lightblue",
578
- font_size=10,
579
- font_weight="bold",
580
- edge_color="gray"
581
- )
582
-
583
- plt.axis("equal")
584
- plt.show()
585
-
586
  def path_finder(destination: int, source: int):
587
  try:
588
  path = nx.shortest_path(G, source=source, target=destination)
@@ -694,6 +680,8 @@ class PathFinding(smolagents.tools.Tool):
694
  def forward(self, destination: str) -> str:
695
  return path_human(destination, source=None)
696
 
 
 
697
  techspark_definition = """
698
  TechSpark is the largest makerspace at CMU (Carnegie Mellon University), located in the College of Engineering. 
699
  Its mission is to promote a vibrant, student-centric making culture to enhance educational, extracurricular, and research activities across the entire campus community.
@@ -740,13 +728,15 @@ agent = smolagents.CodeAgent(
740
  verbosity_level=2, # show steps in logs for class demo
741
  )
742
 
 
 
743
  with gr.Blocks() as demo:
744
 
745
  # Centered title and description using HTML
746
  gr.HTML("""
747
  <div style="text-align: center; font-family: 'Arial', sans-serif;">
748
  <h1 style="color:#1f77b4; margin-bottom: 20px; font-weight: 300;">
749
- 🤖 TechSpark AI Assistant
750
  </h1>
751
 
752
  <p style="margin-top: 0; font-weight: 300; font-size: 16px; color:#555;">
 
1
+ # Imports
2
+
3
  import os
4
  import json
5
  import smolagents
 
12
  import openai
13
  from langchain_community.utilities.wikipedia import WikipediaAPIWrapper
14
  import gradio as gr
15
+
16
+ # Settings
17
 
18
  REPO_ID_TECHSPARK_STAFF = "aslan-ng/CMU_TechSpark_Staff"
19
  REPO_ID_TECHSPARK_COURSES = "aslan-ng/CMU_TechSpark_Courses"
 
26
 
27
  NUMERIC_PROFILE = ["Laser Cutting", "Wood Working", "Wood CNC", "Metal Machining", "Metal CNC", "3D Printer", "Welding", "Electronics"]
28
 
29
+ # Load Data
30
+
31
  def load_data_from_huggingface():
32
  """
33
  Loads data from HuggingFace.
 
56
 
57
  staff_df, courses_df, tools_df, nodes_df, edges_df = load_data_from_huggingface()
58
 
59
+ # General Functions
60
+
61
  def vector_1st_distance(x: list, y: list):
62
  """
63
  Calculate the average 1st distance between two vectors.
 
108
  y.append(electronics)
109
  return vector_1st_distance(x, y)
110
 
111
+ # Staff Functions
112
+
113
  def all_staff():
114
  """
115
  Return a list of all staff.
 
254
  staff_profiles = [get_staff_profile(name) for name in names]
255
  return json.dumps(staff_profiles)
256
 
257
+ # Course Functions
258
+
259
  def all_courses_code():
260
  """
261
  Return a list of all course codes.
 
416
  options = [get_course_profile(course) for course in matches]
417
  return json.dumps(options)
418
 
419
+ # Machines and Tools Functions
420
+
421
  def all_tools():
422
  """
423
  Return a list of all tools and machines.
 
500
  def forward(self, name: str) -> str:
501
  return json.dumps(is_tool_accessible(name))
502
 
503
+ # Wikipedia Functions
504
+
505
  class WikipediaSearch(smolagents.Tool):
506
  """
507
  Create tool for searching Wikipedia
 
520
  answer = wikipedia_api.run(query)
521
  return answer
522
 
523
+ # Map Functions
524
+
525
  def all_nodes():
526
  """
527
  Return a list of all nodes name.
 
569
 
570
  G = load_graph(nodes_df, edges_df)
571
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
572
  def path_finder(destination: int, source: int):
573
  try:
574
  path = nx.shortest_path(G, source=source, target=destination)
 
680
  def forward(self, destination: str) -> str:
681
  return path_human(destination, source=None)
682
 
683
+ # Agent
684
+
685
  techspark_definition = """
686
  TechSpark is the largest makerspace at CMU (Carnegie Mellon University), located in the College of Engineering. 
687
  Its mission is to promote a vibrant, student-centric making culture to enhance educational, extracurricular, and research activities across the entire campus community.
 
728
  verbosity_level=2, # show steps in logs for class demo
729
  )
730
 
731
+ # UI
732
+
733
  with gr.Blocks() as demo:
734
 
735
  # Centered title and description using HTML
736
  gr.HTML("""
737
  <div style="text-align: center; font-family: 'Arial', sans-serif;">
738
  <h1 style="color:#1f77b4; margin-bottom: 20px; font-weight: 300;">
739
+ 🔧 TechSpark AI Assistant 🤖
740
  </h1>
741
 
742
  <p style="margin-top: 0; font-weight: 300; font-size: 16px; color:#555;">