Spaces:
Running
Running
File size: 4,498 Bytes
4834d56 0fb21a5 8fe992b 8ca7148 ecffba9 8ca7148 0fb21a5 8ca7148 9b5b26a 8ca7148 ae7a494 4834d56 8ca7148 4834d56 0fb21a5 8ca7148 0fb21a5 ecffba9 8ca7148 4834d56 0fb21a5 8ca7148 0fb21a5 8ca7148 8fe992b 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 |
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) |