Spaces:
Sleeping
Sleeping
Commit
·
40aea7b
1
Parent(s):
285e5a3
Fix FastAPI for Hugging Face Spaces
Browse files
api.py
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
from fastapi import FastAPI
|
2 |
from utils import process_news
|
|
|
3 |
|
4 |
app = FastAPI(title="News Summarization & TTS API")
|
5 |
|
@@ -20,6 +21,8 @@ def get_news(company_name: str):
|
|
20 |
"""
|
21 |
return process_news(company_name)
|
22 |
|
|
|
23 |
if __name__ == "__main__":
|
|
|
24 |
import uvicorn
|
25 |
-
uvicorn.run(app, host="0.0.0.0", port=
|
|
|
1 |
from fastapi import FastAPI
|
2 |
from utils import process_news
|
3 |
+
import os
|
4 |
|
5 |
app = FastAPI(title="News Summarization & TTS API")
|
6 |
|
|
|
21 |
"""
|
22 |
return process_news(company_name)
|
23 |
|
24 |
+
# Hugging Face automatically runs this service
|
25 |
if __name__ == "__main__":
|
26 |
+
port = int(os.getenv("PORT", 7860)) # Hugging Face assigns a dynamic port
|
27 |
import uvicorn
|
28 |
+
uvicorn.run(app, host="0.0.0.0", port=port)
|
app.py
CHANGED
@@ -2,29 +2,9 @@ import os
|
|
2 |
import time
|
3 |
import requests
|
4 |
import streamlit as st
|
5 |
-
import uvicorn # Add this
|
6 |
-
from fastapi import FastAPI
|
7 |
-
from utils import process_news
|
8 |
|
9 |
-
|
10 |
-
|
11 |
-
spacy.load("en_core_web_sm")
|
12 |
-
except OSError:
|
13 |
-
os.system("python -m spacy download en_core_web_sm")
|
14 |
-
|
15 |
-
# FastAPI app setup
|
16 |
-
api = FastAPI(title="News Summarization & TTS API")
|
17 |
-
|
18 |
-
@api.get("/")
|
19 |
-
def read_root():
|
20 |
-
return {"message": "Welcome to the News Summarization & TTS API"}
|
21 |
-
|
22 |
-
@api.get("/news/{company_name}")
|
23 |
-
def get_news(company_name: str):
|
24 |
-
return process_news(company_name)
|
25 |
-
|
26 |
-
# Streamlit app setup
|
27 |
-
API_URL = "http://127.0.0.1:8000" # Use localhost instead of 0.0.0.0
|
28 |
|
29 |
st.title("News Summarization and Hindi TTS Application")
|
30 |
company = st.text_input("Enter Company Name", "")
|
@@ -34,7 +14,6 @@ if st.button("Fetch News"):
|
|
34 |
st.warning("Please enter a valid company name.")
|
35 |
else:
|
36 |
with st.spinner("Fetching and processing news..."):
|
37 |
-
time.sleep(2) # Give FastAPI some time to start
|
38 |
try:
|
39 |
response = requests.get(f"{API_URL}/news/{company}")
|
40 |
if response.status_code == 200:
|
@@ -71,10 +50,5 @@ if st.button("Fetch News"):
|
|
71 |
st.error("Audio file not found or TTS generation failed.")
|
72 |
else:
|
73 |
st.error("Failed to fetch news from the API. Please try again.")
|
74 |
-
except requests.exceptions.
|
75 |
-
st.error("API
|
76 |
-
|
77 |
-
# 🛠️ **Start FastAPI Server**
|
78 |
-
if __name__ == "__main__":
|
79 |
-
import threading
|
80 |
-
threading.Thread(target=lambda: uvicorn.run(api, host="127.0.0.1", port=8000), daemon=True).start()
|
|
|
2 |
import time
|
3 |
import requests
|
4 |
import streamlit as st
|
|
|
|
|
|
|
5 |
|
6 |
+
# 🔥 Change this to match your Hugging Face Space name
|
7 |
+
API_URL = "https://news-summarise-tts.hf.space" # Replace with your actual Space URL
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
st.title("News Summarization and Hindi TTS Application")
|
10 |
company = st.text_input("Enter Company Name", "")
|
|
|
14 |
st.warning("Please enter a valid company name.")
|
15 |
else:
|
16 |
with st.spinner("Fetching and processing news..."):
|
|
|
17 |
try:
|
18 |
response = requests.get(f"{API_URL}/news/{company}")
|
19 |
if response.status_code == 200:
|
|
|
50 |
st.error("Audio file not found or TTS generation failed.")
|
51 |
else:
|
52 |
st.error("Failed to fetch news from the API. Please try again.")
|
53 |
+
except requests.exceptions.RequestException as e:
|
54 |
+
st.error(f"API error: {e}")
|
|
|
|
|
|
|
|
|
|