Spaces:
Running
Running
| import gradio as gr | |
| from textblob import TextBlob | |
| import gradio as gr | |
| from textblob import TextBlob | |
| import requests | |
| from bs4 import BeautifulSoup | |
| def get_nation_issues(NATION, X_PASSWORD): | |
| url = f"https://www.nationstates.net/cgi-bin/api.cgi" | |
| headers = { | |
| "X-Password": X_PASSWORD, | |
| "User-Agent": "Nationstate LLM" | |
| } | |
| params = { | |
| "nation": NATION, | |
| "q": "issues" | |
| } | |
| response = requests.get(url, headers=headers, params=params) | |
| return response.text | |
| def get_nation_issues_JSON(nation, password): | |
| """ | |
| Fetches and reformats issues from the NationStates API. | |
| Args: | |
| nation (str): The nation name. | |
| password (str): The API password. | |
| Returns: | |
| dict: A dictionary containing the nation ID and a list of issue details. | |
| """ | |
| # Fetch raw SML/XML data | |
| sml_data = get_nation_issues(nation, password) | |
| # Parse the SML/XML | |
| soup = BeautifulSoup(sml_data, "xml") | |
| # Extract nation info | |
| nation_elem = soup.find("NATION") | |
| nation_id = nation_elem.get("id") if nation_elem else "Unknown" | |
| # List to store issues | |
| issues_list = [] | |
| # Extract issues | |
| if nation_elem: | |
| for issue in nation_elem.find_all("ISSUE"): | |
| issue_data = { | |
| "issue_id": issue.get("id", "Unknown"), | |
| "title": issue.TITLE.get_text() if issue.TITLE else "No title available", | |
| "text": issue.TEXT.get_text() if issue.TEXT else "No text available", | |
| "author": issue.AUTHOR.get_text() if issue.AUTHOR else "Unknown author", | |
| "editor": issue.EDITOR.get_text() if issue.EDITOR else "Unknown editor", | |
| "pic1": issue.PIC1.get_text() if issue.PIC1 else "No image 1", | |
| "pic2": issue.PIC2.get_text() if issue.PIC2 else "No image 2", | |
| "options": [] | |
| } | |
| # Extract options within the issue | |
| for option in issue.find_all("OPTION"): | |
| option_data = { | |
| "option_id": option.get("id", "Unknown"), | |
| "text": option.get_text() if option else "No option text" | |
| } | |
| issue_data["options"].append(option_data) | |
| # Add issue data to list | |
| issues_list.append(issue_data) | |
| return {"nation_id": nation_id, "issues": issues_list} | |
| def address_nation_issues(NATION, X_PASSWORD, issue_id, option_id): | |
| """ | |
| Address a specific issue for a nation in NationStates API. | |
| Args: | |
| NATION (str): The nation name. | |
| X_PASSWORD (str): The API password. | |
| issue_id (int): The issue ID to address. | |
| option_id (int): The chosen option ID for the issue. | |
| Returns: | |
| str: API response text. | |
| """ | |
| url = "https://www.nationstates.net/cgi-bin/api.cgi" | |
| headers = { | |
| "X-Password": X_PASSWORD, | |
| "User-Agent": "Nationstate LLM" | |
| } | |
| data = { | |
| "nation": NATION, | |
| "c": "issue", | |
| "issue": issue_id, | |
| "option": option_id | |
| } | |
| response = requests.post(url, headers=headers, data=data) | |
| return response.text | |
| # Create the Gradio interface | |
| get_nation_issues_JSON_interface = gr.Interface( | |
| fn=get_nation_issues_JSON, | |
| inputs=[ | |
| gr.Textbox(label="Nation Name", placeholder="Enter the nation name"), | |
| gr.Textbox(label="API Password", placeholder="Enter your API password"), | |
| ], | |
| outputs=gr.JSON(), | |
| title="Get Nation Issues as JSON", | |
| description="Fetch and reformat issues from the NationStates API", | |
| api_name="get_nation_issues_JSON", | |
| ) | |
| address_nation_issues_interface = gr.Interface( | |
| fn=address_nation_issues, | |
| inputs=[ | |
| gr.Textbox(label="Nation Name", placeholder="Enter the nation name"), | |
| gr.Textbox(label="API Password", placeholder="Enter your API password"), | |
| gr.Textbox(label="Issue ID", placeholder="Enter the issue ID to address"), | |
| gr.Textbox(label="Option ID", placeholder="Enter the option ID to choose") | |
| ], | |
| outputs=gr.Textbox(label="Response"), | |
| title="Address Nation Issues", | |
| description="Address a specific issue for a nation in NationStates API", | |
| api_name="address_nation_issues", | |
| ) | |
| demo = gr.TabbedInterface( | |
| interface_list=[get_nation_issues_JSON_interface, address_nation_issues_interface], | |
| tab_names=["Get Nation Issues JSON", "Address Nation Issues"]) | |
| # Launch the interface and MCP server | |
| if __name__ == "__main__": | |
| demo.launch(mcp_server=True) |