File size: 3,935 Bytes
95f8d1c
 
 
 
 
 
 
 
 
 
 
 
 
 
90790c1
 
 
95f8d1c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2cc4827
 
95f8d1c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os, json, requests
import gradio as gr
from dotenv import load_dotenv

# ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ๋กœ๋“œ
load_dotenv(os.path.join(os.path.dirname(__file__), "..", "backend", ".env"), override=True)

BACKEND = os.getenv('BACKEND_URL','http://localhost:9000')

with gr.Blocks(title='PersonaMate Pro (OAuth + Simplified UI)') as demo:
    gr.Markdown('## PersonaMate Pro โ€” OAuth ์ˆ˜์ง‘ + ์ถ”์ฒœ UI')
    with gr.Row():
        with gr.Column(scale=1):
            gr.Markdown('### 1) OAuth ๋กœ๊ทธ์ธ')
            google_html = gr.HTML(f'<a href="{BACKEND}/oauth/google/start" target="_blank">Google (YouTube) ๋กœ๊ทธ์ธ ์—ด๊ธฐ</a>')
            instagram_html = gr.HTML(f'<a href="{BACKEND}/oauth/instagram/start" target="_blank">Instagram ๋กœ๊ทธ์ธ ์—ด๊ธฐ</a>')
            x_html = gr.HTML(f'<a href="{BACKEND}/oauth/x/start" target="_blank">X (Twitter) ๋กœ๊ทธ์ธ ์—ด๊ธฐ</a>')
        with gr.Column(scale=2):
            gr.Markdown('### 2) ์ž๋™ ์ˆ˜์ง‘')
            yt_chk=gr.Checkbox(label='YouTube ๊ตฌ๋… ๋ชฉ๋ก ์‚ฌ์šฉ', value=True)
            ig_chk=gr.Checkbox(label='Instagram ํ•ด์‹œํƒœ๊ทธ ์‚ฌ์šฉ', value=False)
            x_chk=gr.Checkbox(label='X ํŒ”๋กœ์ž‰ ์‚ฌ์šฉ์ž๋ช… ์‚ฌ์šฉ', value=False)
            fetch_btn=gr.Button('๋‚ด ๊ณ„์ •์—์„œ ๋ฐ์ดํ„ฐ ์ˆ˜์ง‘')
            fetch_result=gr.JSON(label="์ˆ˜์ง‘๋œ ๋ฐ์ดํ„ฐ ๋ฏธ๋ฆฌ๋ณด๊ธฐ")
    with gr.Row():
        with gr.Column(scale=1):
            gr.Markdown('### 3) ์ž…๋ ฅ/MBTI')
            yt_text=gr.Textbox(lines=6,label='์œ ํŠœ๋ธŒ ๊ตฌ๋… (์ˆ˜์ง‘/์ˆ˜๋™ ํ˜ผ์šฉ)')
            sns_text=gr.Textbox(lines=6,label='SNS ํ‚ค์›Œ๋“œ/๊ณ„์ • (์ˆ˜์ง‘/์ˆ˜๋™ ํ˜ผ์šฉ)')
            mbti=gr.Dropdown(choices=['ISTJ','ISFJ','INFJ','INTJ','ISTP','ISFP','INFP','INTP','ESTP','ESFP','ENFP','ENTP','ESTJ','ESFJ','ENFJ','ENTJ'], value='ENFP', label='MBTI')
            use_openai=gr.Checkbox(label='OpenAI ์ž„๋ฒ ๋”ฉ ์‚ฌ์šฉ', value=True)
            run_btn=gr.Button('๋ถ„์„ & ์ถ”์ฒœ ์‹คํ–‰', variant='primary')
            send_email_btn=gr.Button("์ถ”์ฒœ ๊ฒฐ๊ณผ ์ด๋ฉ”์ผ๋กœ ๋ณด๋‚ด๊ธฐ (Gmail)")
        with gr.Column(scale=3):
            gr.Markdown('### 4) ์ถ”์ฒœ ๊ฒฐ๊ณผ')
            result_table=gr.Dataframe(headers=["์ฑ„๋„ ์ด๋ฆ„","์‚ฌ์ดํŠธ ์ฃผ์†Œ"], row_count=10, col_count=2)

    # ๋ฒ„ํŠผ ๋™์ž‘ ์—ฐ๊ฒฐ ์ œ๊ฑฐ (Hugging Face์—์„œ๋Š” webbrowser.open ์‚ฌ์šฉ ๋ถˆ๊ฐ€)
    # ๋Œ€์‹  gr.Link ์ปดํฌ๋„ŒํŠธ๋กœ ๋Œ€์ฒด

    def fetch_data_fn():
        try:
            res = requests.get(f"{BACKEND}/fetch_data", timeout=60)
            res.raise_for_status()
            return res.json()
        except Exception as e:
            return {"error": str(e)}
    fetch_btn.click(fetch_data_fn, inputs=[], outputs=[fetch_result])

    def _run(yt, sns, mbti, use_openai):
        try:
            payload = {
                "youtube_subscriptions": [s.strip() for s in yt.splitlines() if s.strip()],
                "sns_keywords": [s.strip() for s in sns.splitlines() if s.strip()],
                "mbti": mbti
            }
            res = requests.post(f"{BACKEND}/youtube/recommendations", json=payload, timeout=120)
            res.raise_for_status()
            data = res.json().get("recommendations", {})
        except Exception as e:
            data = {"youtube":[{"name":"์ถ”์ฒœ ์‹คํŒจ","url":str(e)}], "web":[]}

        rows = []
        youtube_list = data.get("youtube", [])
        web_list = data.get("web", [])
        if not youtube_list and not web_list:
            # fallback: ์ตœ์†Œํ•œ 1๊ฐœ๋ผ๋„ ํ‘œ์‹œ
            youtube_list = [{"name":"์ถ”์ฒœ ์‹คํŒจ","url":"http://youtube.com"}]
            web_list = [{"name":"์ถ”์ฒœ ์‹คํŒจ","url":"http://example.com"}]

        for c in youtube_list + web_list:
            rows.append([
                c.get("name",""),
                c.get("url","")
            ])
        return rows

    run_btn.click(_run, [yt_text, sns_text, mbti, use_openai], [result_table])

if __name__=='__main__':
    demo.launch()