Spaces:
Sleeping
Sleeping
Download app.py from TracyAI/Tracy: direct link, hf CLI and curl.
- Browser
- Download file 6.36 kB
-
https://huggingface.co/spaces/TracyAI/Tracy/resolve/main/app.py
- Command line
-
hf download hf://spaces/TracyAI/Tracy/app.py
-
curl -L -o app.py https://huggingface.co/spaces/TracyAI/Tracy/resolve/main/app.py
6.36 kB
| from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool | |
| import datetime | |
| import requests | |
| import pytz | |
| import yaml | |
| from tools.visit_webpage import VisitWebpageTool | |
| from tools.final_answer import FinalAnswerTool | |
| from Gradio_UI import GradioUI | |
| from typing import List, Dict | |
| from bs4 import BeautifulSoup | |
| def search_terms_of_service(company_name: str) -> Dict: | |
| """Search for a company's Terms of Service link | |
| Args: | |
| company_name: Name of the organization/company to search for | |
| Returns: | |
| Dictionary containing company name and link to their Terms of Service | |
| """ | |
| try: | |
| # API URL | |
| # API, ki poskrbi za komunikacijo s podatkovno bazo se poganja lokalno | |
| # Da omogočimo javni dostop do API se uporabi ngrok URL povezava, ki se ob vsakem zagonu spremeni | |
| # Posledično je treba ngrok URL stalno posodabljati | |
| api_base = "https://3aec-176-76-224-7.ngrok-free.app" | |
| # API, ki poskrbi za komunikacijo s podatkovno bazo s | |
| # API request naredimo | |
| url = f"{api_base}/company/{company_name}" | |
| print(f"Request URL: {url}") | |
| response = requests.get(url) | |
| print(f"Response status: {response.status_code}") | |
| print(f"Response body preview: {response.text[:200]}...") | |
| if response.status_code == 404: #HTTP error! | |
| return {"error": f"Company '{company_name}' not found in the database"} | |
| response.raise_for_status() | |
| data = response.json() | |
| # ali obstaja več linkov? | |
| tos_link = None | |
| for link in data['secondary_links']: | |
| if 'terms' in link.lower(): | |
| tos_link = link | |
| break | |
| # If no link with "terms" found, fall back to first link | |
| if not tos_link: | |
| tos_link = data["secondary_links"][0] | |
| print(f"Warning: No link containing 'terms' found, using first link: {tos_link}") | |
| # returnamo ime in link, lahko jih več | |
| result = { | |
| "company_name": data["name"], | |
| "tos_link": tos_link, | |
| "all_links": data["secondary_links"] # več linkov | |
| } | |
| return result | |
| except requests.exceptions.RequestException as e: | |
| error_msg = f"API Error: {str(e)}" | |
| if hasattr(e, 'response') and e.response is not None: | |
| error_msg += f" (Status code: {e.response.status_code})" | |
| if hasattr(e.response, 'text'): | |
| error_msg += f" - {e.response.text[:200]}" | |
| print(error_msg) # debug, da prizna, če se zmoti | |
| return {"error": error_msg} | |
| except Exception as e: | |
| error_msg = f"Unexpected error: {str(e)}" | |
| print(error_msg) # debug, da AI prizna, če se zmoti | |
| return {"error": error_msg} | |
| def extract_tos_content(company_name: str) -> Dict: | |
| """ | |
| Search for a company's Terms of Service, access the link, and extract raw content from GitLab repository. | |
| Args: | |
| company_name: Name of the organization/company to search for | |
| Returns: | |
| Dictionary containing company name, link, and extracted content | |
| """ | |
| tos_info = search_terms_of_service(company_name) | |
| # error | |
| if "error" in tos_info: | |
| return tos_info | |
| try: | |
| # link od zgoraj | |
| tos_link = tos_info["tos_link"] | |
| print(f"Accessing TOS link: {tos_link}") | |
| # Gitlab repo | |
| if "code.europa.eu" in tos_link and "/-/blob/" in tos_link: | |
| # spremeni '/-/blob/' z '/-/raw/' | |
| raw_url = tos_link.replace("/-/blob/", "/-/raw/") | |
| if "?" in raw_url: | |
| raw_url = raw_url.split("?")[0] | |
| print(f"Accessing raw content URL: {raw_url}") | |
| # request za raw data iz GItlabaa | |
| response = requests.get(raw_url) | |
| response.raise_for_status() | |
| return { | |
| "company_name": tos_info["company_name"], | |
| "tos_link": tos_link, | |
| "raw_url": raw_url, | |
| "content": response.text, # cel raw data fajl | |
| "content_type": "markdown" if raw_url.endswith(".md") else "text" | |
| } | |
| response = requests.get(tos_link) | |
| response.raise_for_status() | |
| soup = BeautifulSoup(response.text, 'html.parser') | |
| content = "" | |
| pre_element = soup.find('pre') | |
| if pre_element: | |
| content = pre_element.get_text() | |
| else: | |
| main_content = soup.find('main') or soup.find('article') or soup.find('body') | |
| if main_content: | |
| content = main_content.get_text(strip=True) | |
| return { | |
| "company_name": tos_info["company_name"], | |
| "tos_link": tos_link, | |
| "content": content, | |
| "note": "Content extracted from HTML page" | |
| } | |
| except requests.exceptions.RequestException as e: | |
| error_msg = f"Error accessing TOS link: {str(e)}" | |
| if hasattr(e, 'response') and e.response is not None: | |
| error_msg += f" (Status code: {e.response.status_code})" | |
| if hasattr(e.response, 'text'): | |
| error_msg += f" - {e.response.text[:200]}" | |
| print(error_msg) | |
| return {"error": error_msg} | |
| except Exception as e: | |
| error_msg = f"Unexpected error while processing content: {str(e)}" | |
| print(error_msg) | |
| return {"error": error_msg} | |
| final_answer = FinalAnswerTool() | |
| # If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder: | |
| # model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud' | |
| model = HfApiModel( | |
| max_tokens=2096, | |
| temperature=0.5, | |
| model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded | |
| #model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud', | |
| custom_role_conversions=None, | |
| ) | |
| with open("prompts.yaml", 'r') as stream: | |
| prompt_templates = yaml.safe_load(stream) | |
| agent = CodeAgent( | |
| model=model, | |
| tools=[final_answer,search_terms_of_service,extract_tos_content], | |
| max_steps=6, | |
| verbosity_level=1, | |
| grammar=None, | |
| planning_interval=None, | |
| name=None, | |
| description=None, | |
| prompt_templates=prompt_templates | |
| ) | |
| GradioUI(agent).launch() |