Spaces:
Sleeping
Sleeping
File size: 7,078 Bytes
4834d56 0fb21a5 8fe992b 8ca7148 8c5974c 8ca7148 8c5974c 8ca7148 8c5974c 8ca7148 8c5974c 8ca7148 ecffba9 8ca7148 0fb21a5 8ca7148 9b5b26a 8ca7148 ae7a494 4834d56 8ca7148 4834d56 0fb21a5 8ca7148 0fb21a5 ecffba9 8ca7148 8c5974c 4834d56 0fb21a5 8c5974c 8ca7148 0fb21a5 8ca7148 8fe992b 8ca7148 8c5974c 8ca7148 0fb21a5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
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)
def get_nation_issues_JSON(sml_data):
"""
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.
"""
# 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}
return get_nation_issues_JSON(response.text)
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)
def reformat_nation_data(xml_data):
"""
Parses and reformats NationStates XML data into a structured dictionary.
Args:
xml_data (str): Raw XML data.
Returns:
dict: A dictionary containing nation details, issue info, rankings, reclassifications, and headlines.
"""
soup = BeautifulSoup(xml_data, "xml")
nation_elem = soup.find("NATION")
nation_id = nation_elem.get("id") if nation_elem else "Unknown"
issue_elem = nation_elem.find("ISSUE") if nation_elem else None
issue_data = {
"issue_id": issue_elem.get("id") if issue_elem else "Unknown",
"choice": issue_elem.get("choice") if issue_elem else "Unknown",
"ok": issue_elem.OK.get_text() if issue_elem and issue_elem.OK else "Unknown",
"description": issue_elem.DESC.get_text() if issue_elem and issue_elem.DESC else "No description available",
"rankings": [],
"reclassifications": [],
"headlines": []
}
# Extract rankings
rankings_elem = issue_elem.find("RANKINGS") if issue_elem else None
if rankings_elem:
for rank in rankings_elem.find_all("RANK"):
issue_data["rankings"].append({
"rank_id": rank.get("id", "Unknown"),
"score": rank.SCORE.get_text() if rank.SCORE else "Unknown",
"change": rank.CHANGE.get_text() if rank.CHANGE else "Unknown",
"pchange": rank.PCHANGE.get_text() if rank.PCHANGE else "Unknown"
})
# Extract reclassifications
reclassifications_elem = issue_elem.find("RECLASSIFICATIONS") if issue_elem else None
if reclassifications_elem:
for reclassify in reclassifications_elem.find_all("RECLASSIFY"):
issue_data["reclassifications"].append({
"type": reclassify.get("type", "Unknown"),
"from": reclassify.FROM.get_text() if reclassify.FROM else "Unknown",
"to": reclassify.TO.get_text() if reclassify.TO else "Unknown"
})
# Extract headlines
headlines_elem = issue_elem.find("HEADLINES") if issue_elem else None
if headlines_elem:
issue_data["headlines"] = [headline.get_text() for headline in headlines_elem.find_all("HEADLINE")]
return {"nation_id": nation_id, "issue": issue_data}
return reformat_nation_data(response.text)
# Create the Gradio interface
get_nation_issues_interface = gr.Interface(
fn=get_nation_issues,
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_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) |