Download app.py from Zooh/Autostock: direct link, hf CLI and curl.
- Browser
- Download file 7.73 kB
-
https://huggingface.co/spaces/Zooh/Autostock/resolve/main/app.py
- Command line
-
hf download hf://spaces/Zooh/Autostock/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Zooh/Autostock/resolve/main/app.py
7.73 kB
| import gradio as gr | |
| import requests | |
| import json | |
| import os | |
| from datetime import datetime | |
| # Perplexity API ์ค์ | |
| PERPLEXITY_API_KEY = os.getenv("PERPLEXITY_API_KEY") # Space ํ๊ฒฝ๋ณ์์์ ๊ฐ์ ธ์ด | |
| PERPLEXITY_URL = "https://api.perplexity.ai/chat/completions" | |
| def call_perplexity_api(ticker, custom_prompt): | |
| """Perplexity API ํธ์ถ ํจ์""" | |
| # ๊ธฐ๋ณธ ํ๋กฌํํธ์ ์ฌ์ฉ์ ํ๋กฌํํธ ๊ฒฐํฉ | |
| full_prompt = f""" | |
| {custom_prompt} | |
| ๋ถ์ํ ์ข ๋ชฉ: {ticker} | |
| ํ์ฌ ์๊ฐ: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')} | |
| FMP, FactSet, Crunchbase์ ์ต์ ์ค์๊ฐ ๋ฐ์ดํฐ๋ฅผ ์ฐธ์กฐํ์ฌ ์ ํํ ์์น๋ก ๋ถ์ํด์ฃผ์ธ์. | |
| ๋ชจ๋ ์ฌ๋ฌด ๋ฐ์ดํฐ์ ๊ธฐ์ ์ ์งํ๋ ์ค์ ๊ฐ์ ์ฌ์ฉํด์ฃผ์ธ์. | |
| """ | |
| headers = { | |
| "Authorization": f"Bearer {PERPLEXITY_API_KEY}", | |
| "Content-Type": "application/json" | |
| } | |
| data = { | |
| "model": "llama-3.1-sonar-large-128k-online", | |
| "messages": [ | |
| { | |
| "role": "system", | |
| "content": "๋น์ ์ ์ ๋ฌธ ๊ธ์ต ๋ถ์๊ฐ์ ๋๋ค. ์ค์๊ฐ ๊ธ์ต ๋ฐ์ดํฐ์ ์ ๊ทผํ์ฌ ์ ํํ ์ฃผ์ ๋ถ์์ ์ ๊ณตํฉ๋๋ค." | |
| }, | |
| { | |
| "role": "user", | |
| "content": full_prompt | |
| } | |
| ], | |
| "temperature": 0.1, | |
| "max_tokens": 4000 | |
| } | |
| try: | |
| response = requests.post(PERPLEXITY_URL, json=data, headers=headers) | |
| if response.status_code == 200: | |
| result = response.json() | |
| return result['choices'][0]['message']['content'] | |
| else: | |
| return f"API ํธ์ถ ์คํจ: {response.status_code} - {response.text}" | |
| except Exception as e: | |
| return f"์ค๋ฅ ๋ฐ์: {str(e)}" | |
| def create_interface(): | |
| """Gradio ์ธํฐํ์ด์ค ์์ฑ""" | |
| # ๊ธฐ๋ณธ ํ๋กฌํํธ ํ ํ๋ฆฟ | |
| default_prompt = """๋น์ ์ ์ ๋ฌธ ๊ธ์ต ๋ถ์๊ฐ์ ๋๋ค. ๋ค์ ํ์์ผ๋ก ์ข ๋ชฉ์ ๋ถ์ํด์ฃผ์ธ์: | |
| ## 00 Snapshot | |
| ์ข ๋ชฉ: {{ticker}} ๏ฝ ๊ฐ๊ฒฉ: ${{price}}({{change}}%) ๏ฝ 52W: ${{high52}}/${{low52}} | |
| ์๊ฐ์ด์ก: ${{market_cap}}B ๏ฝ ๊ฑฐ๋๋: {{volume}}M ๏ฝ ์ ๊ณ: {{industry}} | |
| ## 01 ํต์ฌ ๋ฐ์ดํฐ | |
| ### ๋ฐธ๋ฅ์์ด์ | |
| - P/E: {{pe}} (๋์ข ์ ๊ณ: {{peer_pe}}) ๏ฝ P/S: {{ps}} | |
| - EV/Sales: {{ev_sales}} ๏ฝ P/B: {{pb}} ๏ฝ ROE: {{roe}}% | |
| ### ๊ธฐ์ ์ ์งํ | |
| - RSI(14): {{rsi}} ๏ฝ MA5: ${{ma5}} ๏ฝ MA20: ${{ma20}} | |
| - MACD: {{macd}} ๏ฝ ๋ณผ๋ฆฐ์ ๋ฐด๋: {{bb_position}} | |
| - Beta: {{beta}} ๏ฝ ๊ฑฐ๋๋(ํ๊ท ๋๋น): {{volume_ratio}}x | |
| ### ์ฑ์ฅ์ฑ | |
| - ๋งค์ถ์ฑ์ฅ๋ฅ (YoY): {{revenue_growth}}% ๏ฝ ์์ด์ต์ฑ์ฅ๋ฅ : {{earnings_growth}}% | |
| - ์ฐ์ CAGR: {{industry_cagr}}% ๏ฝ ๋ง์ง: {{net_margin}}% | |
| ## 02 ๊ฐ์ฑ๋ถ์ (Perplexity ๊ธฐ๋ฐ) | |
| - ๋ด์ค ๊ฐ์ฑ(24H): {{news_sentiment}}/10 | |
| - ์์ ๋ฏธ๋์ด: {{social_sentiment}}/10 | |
| - ์ ๋๋ฆฌ์คํธ: ๋งค์{{buy_ratings}}/๋ณด์ {{hold_ratings}}/๋งค๋{{sell_ratings}} | |
| - **์ข ํฉ ๊ฐ์ฑ**: {{final_sentiment}}/10 | |
| ## 03 ์ ํ๊ฐ ์ ์ ๊ณ์ฐ (0~10) | |
| - V(๋ฐธ๋ฅ): {{v_score}}/10 = P/E์ ์ร0.5 + P/S์ ์ร0.5 | |
| - T(๊ธฐ์ ): {{t_score}}/10 = RSI์ ์ร0.4 + MA์ ์ร0.6 | |
| - G(์ฑ์ฅ): {{g_score}}/10 = ๋งค์ถ์ฑ์ฅร0.6 + ๋ง์งร0.4 | |
| - S(๊ฐ์ฑ): {{s_score}}/10 = ์ข ํฉ๊ฐ์ฑ์ ์ | |
| - M(๋ชจ๋ฉํ ): {{m_score}}/10 = 1๊ฐ์ ์์ต๋ฅ ๊ธฐ๋ฐ | |
| **์ ํ๊ฐ ์ข ํฉ์ ์: {{total_score}}/10** = (Vร0.25 + Tร0.2 + Gร0.2 + Sร0.2 + Mร0.15) | |
| ## 04 ๋ฆฌ์คํฌ ๋ถ์ | |
| - ์ฌ๋ฌด๊ฑด์ ์ฑ: {{financial_risk}}/10 ๏ฝ ์ ๋์ฑ: {{liquidity_risk}}/10 | |
| - ์ ๊ณํฌ์ง์ : {{industry_risk}}/10 ๏ฝ ๊ฑฐ์๊ฒฝ์ : {{macro_risk}}/10 | |
| - **์ข ํฉ ๋ฆฌ์คํฌ**: {{total_risk}}/10 (๋ฎ์์๋ก ์ข์) | |
| ## 05 ์ต์ข ํ๋จ | |
| ### ์ ์ ๊ธฐ๋ฐ ํ๋จ | |
| - **์ ํ๊ฐ**: {{uv_action}} (โฅ7: BUY, 5-7: HOLD, <5: SELL) | |
| - **๋ฆฌ์คํฌ**: {{risk_level}} (โค4: LOW, 4-7: MED, โฅ7: HIGH) | |
| ### **์ต์ข ๋งค๋งค ํ๋จ: {{final_action}}** | |
| ### ์คํ ๊ฐ์ด๋ | |
| - **์ง์ ๋ชฉํ๊ฐ**: ${{entry_price}} (ํ์ฌ๊ฐ ๋๋น {{entry_discount}}%) | |
| - **1์ฐจ ๋ชฉํ๊ฐ**: ${{target1}} (+{{upside1}}%) | |
| - **2์ฐจ ๋ชฉํ๊ฐ**: ${{target2}} (+{{upside2}}%) | |
| - **์์ ๊ฐ**: ${{stop_loss}} (-{{stop_percent}}%) | |
| - **์์ ๋ณด์ ๊ธฐ๊ฐ**: {{holding_days}}์ผ | |
| ### ํฌ์ ๋ ผ๋ฆฌ | |
| **์์น ์์ธ**: {{upside_factors}} | |
| **๋ฆฌ์คํฌ ์์ธ**: {{risk_factors}} | |
| **ํต์ฌ ๋ชจ๋ํฐ๋ง**: {{monitoring_points}} | |
| --- | |
| ๋ถ์ ์๊ฐ: {{timestamp}} | |
| ๋ค์ ์ ๋ฐ์ดํธ: {{next_update}}""" | |
| with gr.Blocks(title="๐ Undervalue Swing Analyzer v7.0", theme=gr.themes.Soft()) as demo: | |
| gr.Markdown(""" | |
| # ๐ Undervalue Swing Analyzer v7.0 | |
| ### Perplexity Finance Pipeline + ์ค์๊ฐ ๋ฅ๋ฆฌ์์น + AI ๊ฐ์ฑ๋ถ์ | |
| **์ค์๊ฐ ๋ฐ์ดํฐ ์์ค**: FMP, FactSet, Crunchbase | |
| """) | |
| with gr.Row(): | |
| with gr.Column(scale=1): | |
| ticker_input = gr.Textbox( | |
| label="๐ ์ข ๋ชฉ ํฐ์ปค", | |
| placeholder="์: AAPL, TSLA, NVDA", | |
| value="AAPL" | |
| ) | |
| analyze_btn = gr.Button( | |
| "๐ ๋ถ์ ์์", | |
| variant="primary", | |
| size="lg" | |
| ) | |
| gr.Markdown("### ๐ ๋ถ์ ํ ํ๋ฆฟ") | |
| prompt_input = gr.Textbox( | |
| label="ํ๋กฌํํธ (์์ ๊ฐ๋ฅ)", | |
| value=default_prompt, | |
| lines=25, | |
| max_lines=30 | |
| ) | |
| with gr.Column(scale=2): | |
| gr.Markdown("### ๐ ๋ถ์ ๊ฒฐ๊ณผ") | |
| result_output = gr.Textbox( | |
| label="", | |
| lines=35, | |
| max_lines=40, | |
| show_copy_button=True | |
| ) | |
| with gr.Row(): | |
| gr.Markdown(""" | |
| ### ๐ก ์ฌ์ฉ ํ | |
| - **ํฐ์ปค ์ ๋ ฅ**: ๋ฏธ๊ตญ ์ฃผ์ ํฐ์ปค๋ฅผ ์ ๋ ฅํ์ธ์ (์: AAPL, GOOGL, MSFT) | |
| - **ํ๋กฌํํธ ์์ **: ์ผ์ชฝ ํ ํ๋ฆฟ์ ์์ ๋กญ๊ฒ ์์ ํ ์ ์์ต๋๋ค | |
| - **์ค์๊ฐ ๋ฐ์ดํฐ**: Perplexity๊ฐ ์ต์ ๊ธ์ต ๋ฐ์ดํฐ๋ฅผ ์๋์ผ๋ก ์์งํฉ๋๋ค | |
| - **๊ฒฐ๊ณผ ๋ณต์ฌ**: ์ฐ์ธก ์๋จ ๋ณต์ฌ ๋ฒํผ์ผ๋ก ๊ฒฐ๊ณผ๋ฅผ ์ฝ๊ฒ ์ ์ฅํ ์ ์์ต๋๋ค | |
| """) | |
| # ์์ ๋ฒํผ๋ค | |
| with gr.Row(): | |
| example_btns = [ | |
| gr.Button("๐ฑ AAPL", size="sm"), | |
| gr.Button("๐ TSLA", size="sm"), | |
| gr.Button("๐ป NVDA", size="sm"), | |
| gr.Button("๐ช AMZN", size="sm"), | |
| gr.Button("๐ GOOGL", size="sm") | |
| ] | |
| # ์ด๋ฒคํธ ํธ๋ค๋ฌ | |
| def analyze_stock(ticker, prompt): | |
| if not PERPLEXITY_API_KEY: | |
| return "โ API ํค๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค. Space ํ๊ฒฝ๋ณ์์ PERPLEXITY_API_KEY๋ฅผ ์ถ๊ฐํด์ฃผ์ธ์." | |
| if not ticker: | |
| return "โ ์ข ๋ชฉ ํฐ์ปค๋ฅผ ์ ๋ ฅํด์ฃผ์ธ์." | |
| return call_perplexity_api(ticker.upper(), prompt) | |
| def set_example_ticker(ticker): | |
| return ticker | |
| # ๋ฒํผ ํด๋ฆญ ์ด๋ฒคํธ | |
| analyze_btn.click( | |
| fn=analyze_stock, | |
| inputs=[ticker_input, prompt_input], | |
| outputs=result_output | |
| ) | |
| # ์์ ๋ฒํผ ์ด๋ฒคํธ | |
| for i, btn in enumerate(example_btns): | |
| tickers = ["AAPL", "TSLA", "NVDA", "AMZN", "GOOGL"] | |
| btn.click( | |
| fn=lambda t=tickers[i]: t, | |
| outputs=ticker_input | |
| ) | |
| return demo | |
| if __name__ == "__main__": | |
| demo = create_interface() | |
| demo.launch() | |