Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -173,7 +173,8 @@ CMD ["npm", "start"]
|
|
173 |
|
174 |
'README.md': """# Compassionate Community
|
175 |
|
176 |
-
A sentiment-based web service using Hugging Face.
|
|
|
177 |
"""
|
178 |
}
|
179 |
|
@@ -188,7 +189,7 @@ PUBLIC_FILES = {
|
|
188 |
</head>
|
189 |
<body>
|
190 |
<h1>Compassionate Community</h1>
|
191 |
-
<p>This page served by Node.js backend.
|
192 |
</body>
|
193 |
</html>
|
194 |
""",
|
@@ -211,20 +212,20 @@ def create_project_files():
|
|
211 |
logs = []
|
212 |
base_dir = "compassionate-community"
|
213 |
try:
|
214 |
-
#
|
215 |
hf_api_token = os.getenv("HF_API_TOKEN", None)
|
216 |
|
217 |
if not os.path.exists(base_dir):
|
218 |
os.makedirs(base_dir)
|
219 |
logs.append(f"Created directory: {base_dir}")
|
220 |
|
221 |
-
# Write main files
|
222 |
for fname, content in MAIN_FILES.items():
|
223 |
fpath = os.path.join(base_dir, fname)
|
224 |
if not os.path.exists(fpath):
|
225 |
-
|
226 |
-
|
227 |
-
|
|
|
228 |
with open(fpath, 'w', encoding='utf-8') as f:
|
229 |
f.write(content)
|
230 |
logs.append(f"Created file: {fpath}")
|
@@ -251,16 +252,20 @@ def create_project_files():
|
|
251 |
|
252 |
def start_node_server():
|
253 |
base_dir = "compassionate-community"
|
|
|
|
|
|
|
|
|
|
|
|
|
254 |
try:
|
255 |
-
# Install dependencies
|
256 |
subprocess.check_call(["npm", "install"], cwd=base_dir)
|
257 |
except Exception as e:
|
258 |
return f"Failed npm install: {e}"
|
259 |
|
260 |
-
# Start the server in the background
|
261 |
subprocess.Popen(["npm", "start"], cwd=base_dir)
|
262 |
|
263 |
-
# Wait for server to
|
264 |
for i in range(30):
|
265 |
try:
|
266 |
r = requests.get("http://localhost:3000")
|
@@ -274,6 +279,12 @@ def start_node_server():
|
|
274 |
|
275 |
|
276 |
def get_sentiment(text):
|
|
|
|
|
|
|
|
|
|
|
|
|
277 |
url = "http://localhost:3000/sentiment"
|
278 |
try:
|
279 |
r = requests.post(url, json={"text": text}, timeout=10)
|
@@ -286,20 +297,11 @@ def get_sentiment(text):
|
|
286 |
return f"Request failed: {e}"
|
287 |
|
288 |
|
289 |
-
# On startup, create files and start the server
|
290 |
setup_logs = create_project_files()
|
291 |
server_logs = start_node_server()
|
292 |
|
293 |
def query_interface(input_text):
|
294 |
-
|
295 |
-
with open("compassionate-community/.env", 'r', encoding='utf-8') as envf:
|
296 |
-
env_content = envf.read()
|
297 |
-
if "YOUR_HF_API_TOKEN_HERE" in env_content:
|
298 |
-
warning = "Warning: HF API Token not set. Please set a token and restart."
|
299 |
-
else:
|
300 |
-
warning = ""
|
301 |
-
sentiment_result = get_sentiment(input_text)
|
302 |
-
return f"{warning}\n{sentiment_result}"
|
303 |
|
304 |
with gr.Blocks() as demo:
|
305 |
gr.Markdown("# Compassionate Community Full Service\n")
|
|
|
173 |
|
174 |
'README.md': """# Compassionate Community
|
175 |
|
176 |
+
A sentiment-based web service using Hugging Face.
|
177 |
+
Add your HF token to .env or set it as a Space secret.
|
178 |
"""
|
179 |
}
|
180 |
|
|
|
189 |
</head>
|
190 |
<body>
|
191 |
<h1>Compassionate Community</h1>
|
192 |
+
<p>This page served by Node.js backend. Use the Gradio interface to test sentiment analysis.</p>
|
193 |
</body>
|
194 |
</html>
|
195 |
""",
|
|
|
212 |
logs = []
|
213 |
base_dir = "compassionate-community"
|
214 |
try:
|
215 |
+
# Check for HF_API_TOKEN from secret (Space)
|
216 |
hf_api_token = os.getenv("HF_API_TOKEN", None)
|
217 |
|
218 |
if not os.path.exists(base_dir):
|
219 |
os.makedirs(base_dir)
|
220 |
logs.append(f"Created directory: {base_dir}")
|
221 |
|
|
|
222 |
for fname, content in MAIN_FILES.items():
|
223 |
fpath = os.path.join(base_dir, fname)
|
224 |
if not os.path.exists(fpath):
|
225 |
+
if fname == '.env':
|
226 |
+
# Insert token if we have one
|
227 |
+
if hf_api_token and "YOUR_HF_API_TOKEN_HERE" in content:
|
228 |
+
content = content.replace("YOUR_HF_API_TOKEN_HERE", hf_api_token)
|
229 |
with open(fpath, 'w', encoding='utf-8') as f:
|
230 |
f.write(content)
|
231 |
logs.append(f"Created file: {fpath}")
|
|
|
252 |
|
253 |
def start_node_server():
|
254 |
base_dir = "compassionate-community"
|
255 |
+
# Check if token is set
|
256 |
+
with open(os.path.join(base_dir, '.env'), 'r', encoding='utf-8') as envf:
|
257 |
+
env_content = envf.read()
|
258 |
+
if "YOUR_HF_API_TOKEN_HERE" in env_content:
|
259 |
+
return "No valid HF_API_TOKEN provided. Please set it as a secret or edit .env."
|
260 |
+
|
261 |
try:
|
|
|
262 |
subprocess.check_call(["npm", "install"], cwd=base_dir)
|
263 |
except Exception as e:
|
264 |
return f"Failed npm install: {e}"
|
265 |
|
|
|
266 |
subprocess.Popen(["npm", "start"], cwd=base_dir)
|
267 |
|
268 |
+
# Wait up to 30s for Node server to start
|
269 |
for i in range(30):
|
270 |
try:
|
271 |
r = requests.get("http://localhost:3000")
|
|
|
279 |
|
280 |
|
281 |
def get_sentiment(text):
|
282 |
+
# Check token again
|
283 |
+
with open("compassionate-community/.env", 'r', encoding='utf-8') as envf:
|
284 |
+
env_content = envf.read()
|
285 |
+
if "YOUR_HF_API_TOKEN_HERE" in env_content:
|
286 |
+
return "Warning: No HF_API_TOKEN set. Cannot perform sentiment analysis."
|
287 |
+
|
288 |
url = "http://localhost:3000/sentiment"
|
289 |
try:
|
290 |
r = requests.post(url, json={"text": text}, timeout=10)
|
|
|
297 |
return f"Request failed: {e}"
|
298 |
|
299 |
|
|
|
300 |
setup_logs = create_project_files()
|
301 |
server_logs = start_node_server()
|
302 |
|
303 |
def query_interface(input_text):
|
304 |
+
return get_sentiment(input_text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
305 |
|
306 |
with gr.Blocks() as demo:
|
307 |
gr.Markdown("# Compassionate Community Full Service\n")
|