Spaces:
Sleeping
Sleeping
beautifulsoup4
Browse files- app.py +119 -16
- requirements.txt +0 -0
app.py
CHANGED
@@ -1,36 +1,139 @@
|
|
1 |
import gradio as gr
|
2 |
from textblob import TextBlob
|
3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
-
|
|
|
6 |
"""
|
7 |
-
|
8 |
|
9 |
Args:
|
10 |
-
|
|
|
|
|
|
|
11 |
|
12 |
Returns:
|
13 |
-
|
14 |
"""
|
15 |
-
blob = TextBlob(text)
|
16 |
-
sentiment = blob.sentiment
|
17 |
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
}
|
23 |
|
|
|
|
|
|
|
|
|
24 |
|
25 |
# Create the Gradio interface
|
26 |
-
|
27 |
-
fn=
|
28 |
-
inputs=
|
|
|
|
|
|
|
29 |
outputs=gr.JSON(),
|
30 |
-
title="
|
31 |
-
description="
|
|
|
32 |
)
|
33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
# Launch the interface and MCP server
|
35 |
if __name__ == "__main__":
|
36 |
demo.launch(mcp_server=True)
|
|
|
1 |
import gradio as gr
|
2 |
from textblob import TextBlob
|
3 |
|
4 |
+
import gradio as gr
|
5 |
+
from textblob import TextBlob
|
6 |
+
import requests
|
7 |
+
from bs4 import BeautifulSoup
|
8 |
+
|
9 |
+
|
10 |
+
def get_nation_issues(NATION, X_PASSWORD):
|
11 |
+
url = f"https://www.nationstates.net/cgi-bin/api.cgi"
|
12 |
+
headers = {
|
13 |
+
"X-Password": X_PASSWORD,
|
14 |
+
"User-Agent": "Nationstate LLM"
|
15 |
+
}
|
16 |
+
params = {
|
17 |
+
"nation": NATION,
|
18 |
+
"q": "issues"
|
19 |
+
}
|
20 |
+
response = requests.get(url, headers=headers, params=params)
|
21 |
+
return response.text
|
22 |
+
|
23 |
+
|
24 |
+
|
25 |
+
def get_nation_issues_JSON(nation, password):
|
26 |
+
"""
|
27 |
+
Fetches and reformats issues from the NationStates API.
|
28 |
+
|
29 |
+
Args:
|
30 |
+
nation (str): The nation name.
|
31 |
+
password (str): The API password.
|
32 |
+
|
33 |
+
Returns:
|
34 |
+
dict: A dictionary containing the nation ID and a list of issue details.
|
35 |
+
"""
|
36 |
+
|
37 |
+
# Fetch raw SML/XML data
|
38 |
+
sml_data = get_nation_issues(nation, password)
|
39 |
+
|
40 |
+
# Parse the SML/XML
|
41 |
+
soup = BeautifulSoup(sml_data, "xml")
|
42 |
+
|
43 |
+
# Extract nation info
|
44 |
+
nation_elem = soup.find("NATION")
|
45 |
+
nation_id = nation_elem.get("id") if nation_elem else "Unknown"
|
46 |
+
|
47 |
+
# List to store issues
|
48 |
+
issues_list = []
|
49 |
+
|
50 |
+
# Extract issues
|
51 |
+
if nation_elem:
|
52 |
+
for issue in nation_elem.find_all("ISSUE"):
|
53 |
+
issue_data = {
|
54 |
+
"issue_id": issue.get("id", "Unknown"),
|
55 |
+
"title": issue.TITLE.get_text() if issue.TITLE else "No title available",
|
56 |
+
"text": issue.TEXT.get_text() if issue.TEXT else "No text available",
|
57 |
+
"author": issue.AUTHOR.get_text() if issue.AUTHOR else "Unknown author",
|
58 |
+
"editor": issue.EDITOR.get_text() if issue.EDITOR else "Unknown editor",
|
59 |
+
"pic1": issue.PIC1.get_text() if issue.PIC1 else "No image 1",
|
60 |
+
"pic2": issue.PIC2.get_text() if issue.PIC2 else "No image 2",
|
61 |
+
"options": []
|
62 |
+
}
|
63 |
+
|
64 |
+
# Extract options within the issue
|
65 |
+
for option in issue.find_all("OPTION"):
|
66 |
+
option_data = {
|
67 |
+
"option_id": option.get("id", "Unknown"),
|
68 |
+
"text": option.get_text() if option else "No option text"
|
69 |
+
}
|
70 |
+
issue_data["options"].append(option_data)
|
71 |
+
|
72 |
+
# Add issue data to list
|
73 |
+
issues_list.append(issue_data)
|
74 |
|
75 |
+
return {"nation_id": nation_id, "issues": issues_list}
|
76 |
+
def address_nation_issues(NATION, X_PASSWORD, issue_id, option_id):
|
77 |
"""
|
78 |
+
Address a specific issue for a nation in NationStates API.
|
79 |
|
80 |
Args:
|
81 |
+
NATION (str): The nation name.
|
82 |
+
X_PASSWORD (str): The API password.
|
83 |
+
issue_id (int): The issue ID to address.
|
84 |
+
option_id (int): The chosen option ID for the issue.
|
85 |
|
86 |
Returns:
|
87 |
+
str: API response text.
|
88 |
"""
|
|
|
|
|
89 |
|
90 |
+
url = "https://www.nationstates.net/cgi-bin/api.cgi"
|
91 |
+
|
92 |
+
headers = {
|
93 |
+
"X-Password": X_PASSWORD,
|
94 |
+
"User-Agent": "Nationstate LLM"
|
95 |
+
}
|
96 |
+
|
97 |
+
data = {
|
98 |
+
"nation": NATION,
|
99 |
+
"c": "issue",
|
100 |
+
"issue": issue_id,
|
101 |
+
"option": option_id
|
102 |
}
|
103 |
|
104 |
+
response = requests.post(url, headers=headers, data=data)
|
105 |
+
|
106 |
+
return response.text
|
107 |
+
|
108 |
|
109 |
# Create the Gradio interface
|
110 |
+
get_nation_issues_JSON_interface = gr.Interface(
|
111 |
+
fn=get_nation_issues_JSON,
|
112 |
+
inputs=[
|
113 |
+
gr.Textbox(label="Nation Name", placeholder="Enter the nation name"),
|
114 |
+
gr.Textbox(label="API Password", placeholder="Enter your API password"),
|
115 |
+
],
|
116 |
outputs=gr.JSON(),
|
117 |
+
title="Get Nation Issues as JSON",
|
118 |
+
description="Fetch and reformat issues from the NationStates API",
|
119 |
+
api_name="get_nation_issues_JSON",
|
120 |
)
|
121 |
+
address_nation_issues_interface = gr.Interface(
|
122 |
+
fn=address_nation_issues,
|
123 |
+
inputs=[
|
124 |
+
gr.Textbox(label="Nation Name", placeholder="Enter the nation name"),
|
125 |
+
gr.Textbox(label="API Password", placeholder="Enter your API password"),
|
126 |
+
gr.Textbox(label="Issue ID", placeholder="Enter the issue ID to address"),
|
127 |
+
gr.Textbox(label="Option ID", placeholder="Enter the option ID to choose")
|
128 |
+
],
|
129 |
+
outputs=gr.Textbox(label="Response"),
|
130 |
+
title="Address Nation Issues",
|
131 |
+
description="Address a specific issue for a nation in NationStates API",
|
132 |
+
api_name="address_nation_issues",
|
133 |
+
)
|
134 |
+
demo = gr.TabbedInterface(
|
135 |
+
interface_list=[get_nation_issues_JSON_interface, address_nation_issues_interface],
|
136 |
+
tab_names=["Get Nation Issues JSON", "Address Nation Issues"])
|
137 |
# Launch the interface and MCP server
|
138 |
if __name__ == "__main__":
|
139 |
demo.launch(mcp_server=True)
|
requirements.txt
CHANGED
Binary files a/requirements.txt and b/requirements.txt differ
|
|