Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files
app.py
CHANGED
|
@@ -1,39 +1,50 @@
|
|
| 1 |
-
import os, requests
|
| 2 |
import gradio as gr
|
| 3 |
from dotenv import load_dotenv
|
|
|
|
| 4 |
|
| 5 |
# 환경 변수 로드 (backend/.env에는 Vercel 배포용 환경이 포함되어 있어야 합니다)
|
| 6 |
load_dotenv(os.path.join(os.path.dirname(__file__), "..", "backend", ".env"), override=True)
|
| 7 |
|
| 8 |
# Vercel 배포된 백엔드 URL (GitHub Space Secrets에 BACKEND_URL 설정)
|
| 9 |
-
BACKEND = os.getenv('BACKEND_URL', 'https://
|
| 10 |
|
| 11 |
-
def fetch_data_fn():
|
|
|
|
| 12 |
try:
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
| 16 |
except Exception as e:
|
| 17 |
return {"error": str(e)}
|
| 18 |
|
| 19 |
-
def run_recommendations(yt, sns, mbti
|
|
|
|
| 20 |
try:
|
| 21 |
payload = {
|
| 22 |
"youtube_subscriptions": [s.strip() for s in yt.splitlines() if s.strip()],
|
| 23 |
"sns_keywords": [s.strip() for s in sns.splitlines() if s.strip()],
|
| 24 |
"mbti": mbti
|
| 25 |
}
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
| 29 |
except Exception as e:
|
| 30 |
-
data = {"youtube":
|
| 31 |
|
| 32 |
rows = []
|
| 33 |
for c in data.get("youtube", []) + data.get("web", []):
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
if not rows:
|
| 36 |
-
rows = [["추천 실패", ""]]
|
| 37 |
return rows
|
| 38 |
|
| 39 |
with gr.Blocks(title='PersonaMate Pro — OAuth 수집 + 추천 UI') as demo:
|
|
@@ -61,14 +72,14 @@ with gr.Blocks(title='PersonaMate Pro — OAuth 수집 + 추천 UI') as demo:
|
|
| 61 |
value='ENFP',
|
| 62 |
label='MBTI'
|
| 63 |
)
|
| 64 |
-
use_openai = gr.Checkbox(label='OpenAI 임베딩 사용', value=True)
|
| 65 |
run_btn = gr.Button('추천 실행', variant='primary')
|
| 66 |
with gr.Column(scale=3):
|
| 67 |
gr.Markdown('### 4) 추천 결과')
|
| 68 |
-
result_table
|
| 69 |
|
| 70 |
fetch_btn.click(fetch_data_fn, inputs=[], outputs=[fetch_result])
|
| 71 |
-
run_btn.click(run_recommendations, [yt_text, sns_text, mbti
|
| 72 |
|
| 73 |
if __name__ == '__main__':
|
| 74 |
demo.launch()
|
|
|
|
| 1 |
+
import os, json, requests
|
| 2 |
import gradio as gr
|
| 3 |
from dotenv import load_dotenv
|
| 4 |
+
import httpx # Import httpx for async client
|
| 5 |
|
| 6 |
# 환경 변수 로드 (backend/.env에는 Vercel 배포용 환경이 포함되어 있어야 합니다)
|
| 7 |
load_dotenv(os.path.join(os.path.dirname(__file__), "..", "backend", ".env"), override=True)
|
| 8 |
|
| 9 |
# Vercel 배포된 백엔드 URL (GitHub Space Secrets에 BACKEND_URL 설정)
|
| 10 |
+
BACKEND = os.getenv('BACKEND_URL', 'https://personamate-kimddols-projects.vercel.app') # Updated to fixed production domain
|
| 11 |
|
| 12 |
+
async def fetch_data_fn(): # Made async
|
| 13 |
+
print("Fetching data endpoint called.")
|
| 14 |
try:
|
| 15 |
+
async with httpx.AsyncClient() as client: # Use async client
|
| 16 |
+
res = await client.get(f"{BACKEND}/fetch_data", timeout=60)
|
| 17 |
+
res.raise_for_status()
|
| 18 |
+
return res.json()
|
| 19 |
except Exception as e:
|
| 20 |
return {"error": str(e)}
|
| 21 |
|
| 22 |
+
async def run_recommendations(yt, sns, mbti): # Removed use_openai
|
| 23 |
+
print("YouTube recommendations endpoint called.")
|
| 24 |
try:
|
| 25 |
payload = {
|
| 26 |
"youtube_subscriptions": [s.strip() for s in yt.splitlines() if s.strip()],
|
| 27 |
"sns_keywords": [s.strip() for s in sns.splitlines() if s.strip()],
|
| 28 |
"mbti": mbti
|
| 29 |
}
|
| 30 |
+
async with httpx.AsyncClient() as client: # Use async client
|
| 31 |
+
res = await client.post(f"{BACKEND}/youtube/recommendations", json=payload, timeout=120)
|
| 32 |
+
res.raise_for_status()
|
| 33 |
+
data = res.json().get("recommendations", {})
|
| 34 |
except Exception as e:
|
| 35 |
+
data = {"youtube":[{"name":"추천 실패","url":str(e)}], "web":[]}
|
| 36 |
|
| 37 |
rows = []
|
| 38 |
for c in data.get("youtube", []) + data.get("web", []):
|
| 39 |
+
# Add reason to the row and make URL clickable
|
| 40 |
+
url_html = f'<a href="{c.get("url","")}" target="_blank">{c.get("url","")}</a>' if c.get("url") else ""
|
| 41 |
+
rows.append([
|
| 42 |
+
c.get("name",""),
|
| 43 |
+
url_html,
|
| 44 |
+
c.get("reason","") # Add reason here
|
| 45 |
+
])
|
| 46 |
if not rows:
|
| 47 |
+
rows = [["추천 실패", "", ""]] # Update fallback for 3 columns
|
| 48 |
return rows
|
| 49 |
|
| 50 |
with gr.Blocks(title='PersonaMate Pro — OAuth 수집 + 추천 UI') as demo:
|
|
|
|
| 72 |
value='ENFP',
|
| 73 |
label='MBTI'
|
| 74 |
)
|
| 75 |
+
# use_openai = gr.Checkbox(label='OpenAI 임베딩 사용', value=True) # Removed
|
| 76 |
run_btn = gr.Button('추천 실행', variant='primary')
|
| 77 |
with gr.Column(scale=3):
|
| 78 |
gr.Markdown('### 4) 추천 결과')
|
| 79 |
+
result_table=gr.Dataframe(headers=["채널 이름","사이트 주소", "추천 사유"], row_count=10, col_count=3) # Updated headers and col_count
|
| 80 |
|
| 81 |
fetch_btn.click(fetch_data_fn, inputs=[], outputs=[fetch_result])
|
| 82 |
+
run_btn.click(run_recommendations, [yt_text, sns_text, mbti], [result_table]) # Removed use_openai from inputs
|
| 83 |
|
| 84 |
if __name__ == '__main__':
|
| 85 |
demo.launch()
|