RalphThings commited on
Commit
115798c
·
verified ·
1 Parent(s): 8b00cd9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -68
app.py CHANGED
@@ -1,7 +1,8 @@
1
  import os, re, requests, pandas as pd, gradio as gr
2
  from langchain_huggingface.llms import HuggingFacePipeline
3
- from langchain_core.tools import tool
4
- from langchain_core.agents import AgentExecutor, JsonOutputParser
 
5
  from youtube_transcript_api import YouTubeTranscriptApi
6
  import chess, chess.engine
7
  from bs4 import BeautifulSoup
@@ -12,12 +13,7 @@ from SPARQLWrapper import SPARQLWrapper, JSON
12
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
13
  HF_TOKEN = os.getenv("HF_TOKEN", None)
14
 
15
- @tool(
16
- name="wiki_get_page",
17
- description="Fetch raw wikitext for a given Wikipedia page title",
18
- inputs={"title": "string"},
19
- output_type="string",
20
- )
21
  def wiki_get_page(title: str) -> str:
22
  API = "https://en.wikipedia.org/w/api.php"
23
  params = {"action": "query", "format": "json", "prop": "revisions", "rvprop": "content", "rvslots": "*", "titles": title}
@@ -25,31 +21,16 @@ def wiki_get_page(title: str) -> str:
25
  page = next(iter(data["query"]["pages"].values()))
26
  return page["revisions"][0]["slots"]["main"]["*"]
27
 
28
- @tool(
29
- name="youtube_transcript",
30
- description="Retrieve transcript for a YouTube video ID",
31
- inputs={"video_id": "string"},
32
- output_type="string",
33
- )
34
  def youtube_transcript(video_id: str) -> str:
35
  transcript = YouTubeTranscriptApi().fetch_transcript(video_id)
36
  return " ".join(t["text"] for t in transcript)
37
 
38
- @tool(
39
- name="reverse_text",
40
- description="Reverse the input string",
41
- inputs={"text": "string"},
42
- output_type="string",
43
- )
44
  def reverse_text(text: str) -> str:
45
  return text[::-1]
46
 
47
- @tool(
48
- name="chess_best_move",
49
- description="Return best move in UCI notation for given FEN",
50
- inputs={"fen": "string", "time_limit": "float"},
51
- output_type="string",
52
- )
53
  def chess_best_move(fen: str, time_limit: float = 0.1) -> str:
54
  board = chess.Board(fen)
55
  engine = chess.engine.SimpleEngine.popen_uci("/usr/bin/stockfish")
@@ -57,12 +38,7 @@ def chess_best_move(fen: str, time_limit: float = 0.1) -> str:
57
  engine.quit()
58
  return result.move.uci()
59
 
60
- @tool(
61
- name="find_non_commutative",
62
- description="Find elements involved in non-commutativity from operation table",
63
- inputs={"table": "dict"},
64
- output_type="list[string]",
65
- )
66
  def find_non_commutative(table: dict) -> list:
67
  elems = set(x for x,_ in table.keys())
68
  bad = set()
@@ -72,56 +48,31 @@ def find_non_commutative(table: dict) -> list:
72
  bad.update([x,y])
73
  return sorted(bad)
74
 
75
- @tool(
76
- name="libretext_extract",
77
- description="Extract text from LibreTexts URL using CSS selector",
78
- inputs={"url": "string", "selector": "string"},
79
- output_type="string",
80
- )
81
  def libretext_extract(url: str, selector: str) -> str:
82
  r = requests.get(url, timeout=10)
83
  soup = BeautifulSoup(r.text, "html.parser")
84
  return soup.select_one(selector).get_text(strip=True)
85
 
86
- @tool(
87
- name="classify_vegetables",
88
- description="Return alphabetized list of vegetables from input list",
89
- inputs={"items": "list[string]"},
90
- output_type="list[string]",
91
- )
92
  def classify_vegetables(items: list) -> list:
93
  VEGETABLE_SET = {"bell pepper","broccoli","celery","green beans","lettuce","zucchini","sweet potatoes"}
94
  return sorted([i for i in items if i in VEGETABLE_SET])
95
 
96
- @tool(
97
- name="execute_code",
98
- description="Execute Python code snippet and return 'output' variable",
99
- inputs={"code": "string"},
100
- output_type="string",
101
- )
102
  def execute_code(code: str) -> str:
103
  local_ns = {}
104
  exec(code, {"__builtins__": {}}, local_ns)
105
  return str(local_ns.get("output", ""))
106
 
107
- @tool(
108
- name="yankee_at_bats_most_walks",
109
- description="Return at bats for Yankee with most walks in given season",
110
- inputs={"year": "int"},
111
- output_type="int",
112
- )
113
  def yankee_at_bats_most_walks(year: int) -> int:
114
  leaders = statsapi.team_leaders("walks", season=year, team=147)
115
  pid = leaders[0]["id"]
116
  stats = statsapi.player_stats(pid, "hitting", "season", season=year)
117
  return stats["batting"][0]["atBats"]
118
 
119
- @tool(
120
- name="least_athletes_olympics",
121
- description="Return IOC code of country with least athletes in given Olympics year",
122
- inputs={"year": "int"},
123
- output_type="string",
124
- )
125
  def least_athletes_olympics(year: int) -> str:
126
  url = f"https://en.wikipedia.org/wiki/{year}_Summer_Olympics"
127
  r = requests.get(url)
@@ -133,12 +84,7 @@ def least_athletes_olympics(year: int) -> str:
133
  candidates = sorted([code for code,count in data if count==min_val])
134
  return candidates[0]
135
 
136
- @tool(
137
- name="get_nasa_award_number",
138
- description="Get NASA award number for a Wikidata QID",
139
- inputs={"qid": "string"},
140
- output_type="string",
141
- )
142
  def get_nasa_award_number(qid: str) -> str:
143
  sparql = SPARQLWrapper("https://query.wikidata.org/sparql")
144
  sparql.setQuery(f'SELECT ?award WHERE {{ wd:{qid} wdt:P496 ?award. }}')
 
1
  import os, re, requests, pandas as pd, gradio as gr
2
  from langchain_huggingface.llms import HuggingFacePipeline
3
+ from langchain.tools import tool
4
+ from langchain_core,output_parsers import JsonOutputParser
5
+ from langchain.agents import AgentExecutor
6
  from youtube_transcript_api import YouTubeTranscriptApi
7
  import chess, chess.engine
8
  from bs4 import BeautifulSoup
 
13
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
14
  HF_TOKEN = os.getenv("HF_TOKEN", None)
15
 
16
+ @tool
 
 
 
 
 
17
  def wiki_get_page(title: str) -> str:
18
  API = "https://en.wikipedia.org/w/api.php"
19
  params = {"action": "query", "format": "json", "prop": "revisions", "rvprop": "content", "rvslots": "*", "titles": title}
 
21
  page = next(iter(data["query"]["pages"].values()))
22
  return page["revisions"][0]["slots"]["main"]["*"]
23
 
24
+ @tool
 
 
 
 
 
25
  def youtube_transcript(video_id: str) -> str:
26
  transcript = YouTubeTranscriptApi().fetch_transcript(video_id)
27
  return " ".join(t["text"] for t in transcript)
28
 
29
+ @tool
 
 
 
 
 
30
  def reverse_text(text: str) -> str:
31
  return text[::-1]
32
 
33
+ @tool
 
 
 
 
 
34
  def chess_best_move(fen: str, time_limit: float = 0.1) -> str:
35
  board = chess.Board(fen)
36
  engine = chess.engine.SimpleEngine.popen_uci("/usr/bin/stockfish")
 
38
  engine.quit()
39
  return result.move.uci()
40
 
41
+ @tool
 
 
 
 
 
42
  def find_non_commutative(table: dict) -> list:
43
  elems = set(x for x,_ in table.keys())
44
  bad = set()
 
48
  bad.update([x,y])
49
  return sorted(bad)
50
 
51
+ @tool
 
 
 
 
 
52
  def libretext_extract(url: str, selector: str) -> str:
53
  r = requests.get(url, timeout=10)
54
  soup = BeautifulSoup(r.text, "html.parser")
55
  return soup.select_one(selector).get_text(strip=True)
56
 
57
+ @tool
 
 
 
 
 
58
  def classify_vegetables(items: list) -> list:
59
  VEGETABLE_SET = {"bell pepper","broccoli","celery","green beans","lettuce","zucchini","sweet potatoes"}
60
  return sorted([i for i in items if i in VEGETABLE_SET])
61
 
62
+ @tool
 
 
 
 
 
63
  def execute_code(code: str) -> str:
64
  local_ns = {}
65
  exec(code, {"__builtins__": {}}, local_ns)
66
  return str(local_ns.get("output", ""))
67
 
68
+ @tool
 
 
 
 
 
69
  def yankee_at_bats_most_walks(year: int) -> int:
70
  leaders = statsapi.team_leaders("walks", season=year, team=147)
71
  pid = leaders[0]["id"]
72
  stats = statsapi.player_stats(pid, "hitting", "season", season=year)
73
  return stats["batting"][0]["atBats"]
74
 
75
+ @tool
 
 
 
 
 
76
  def least_athletes_olympics(year: int) -> str:
77
  url = f"https://en.wikipedia.org/wiki/{year}_Summer_Olympics"
78
  r = requests.get(url)
 
84
  candidates = sorted([code for code,count in data if count==min_val])
85
  return candidates[0]
86
 
87
+ @tool
 
 
 
 
 
88
  def get_nasa_award_number(qid: str) -> str:
89
  sparql = SPARQLWrapper("https://query.wikidata.org/sparql")
90
  sparql.setQuery(f'SELECT ?award WHERE {{ wd:{qid} wdt:P496 ?award. }}')