Update app.py
Browse files
app.py
CHANGED
@@ -64,39 +64,37 @@ plt.legend(prop=font_prop)
|
|
64 |
# μ΄ν μ½λ μ€ν
|
65 |
import requests
|
66 |
import gradio as gr
|
67 |
-
import yfinance as yf
|
68 |
-
import matplotlib.pyplot as plt
|
69 |
import io
|
70 |
from PIL import Image
|
71 |
from datetime import datetime, timedelta
|
72 |
from openai import OpenAI
|
73 |
|
74 |
# Perplexity AI API μ€μ
|
75 |
-
API_KEY = "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"
|
76 |
|
77 |
# λ΄μ€ μμ½μ κ°μ Έμ€λ ν¨μ
|
78 |
def get_real_news_summary(company, date):
|
79 |
# OpenAI ν΄λΌμ΄μΈνΈ μ΄κΈ°ν
|
80 |
client = OpenAI(api_key=API_KEY, base_url="https://api.perplexity.ai")
|
81 |
-
|
82 |
# API μμ²μ μν λ©μμ§ κ΅¬μ± - νκ΅μ΄λ‘λ§ μλ΅μ λ°λλ‘ μ§μ
|
83 |
messages = [
|
84 |
{"role": "system", "content": "You are a helpful assistant that summarizes stock news strictly in Korean."},
|
85 |
{"role": "user", "content": f"Summarize the latest stock news for {company} on {date} in Korean. If there's no specific news for that date, provide the most recent relevant information in Korean."}
|
86 |
]
|
87 |
-
|
88 |
try:
|
89 |
# API μμ²
|
90 |
response = client.chat.completions.create(
|
91 |
model="llama-3.1-sonar-large-128k-online",
|
92 |
messages=messages
|
93 |
)
|
94 |
-
|
95 |
# μλ΅μμ μμ½ μΆμΆ
|
96 |
summary = response.choices[0].message.content
|
97 |
|
98 |
# νκΈ, μ«μ, 곡백, νΉμ κΈ°νΈλ§ λ¨κΈ°λ μ κ· ννμ
|
99 |
-
korean_only_summary = re.sub(r'[^\w\s#.,!%()
|
100 |
|
101 |
# ##λ‘ μμνλ λΆλΆμ **μΌλ‘ κ°μΈμ Bold μ²λ¦¬
|
102 |
formatted_summary = re.sub(r'##\s*(.+)', r'**\1**', korean_only_summary)
|
@@ -109,6 +107,13 @@ def get_real_news_summary(company, date):
|
|
109 |
def handle_click(input_value, date_clicked):
|
110 |
return get_real_news_summary(input_value, date_clicked)
|
111 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
112 |
# μ’
λͺ©λͺ
κ³Ό ν°μ»€λ₯Ό λ§€ννλ λμ
λ리 νμ₯
|
113 |
name_to_ticker = {
|
114 |
"μΌμ±μ μ": "005930.KS",
|
@@ -188,11 +193,11 @@ with gr.Blocks() as demo:
|
|
188 |
submit_btn = gr.Button("Submit")
|
189 |
|
190 |
# μμ (μ΄μ λ μ΄μμμΌλ‘ 볡μ)
|
191 |
-
examples = [["SKλ°μ΄μ€ν"],
|
192 |
-
["λμ€λ₯ μμ΄ 1μ"],
|
193 |
-
["λμ€λ₯ μ μ½μ£Ό μμ΄ 1μ"],
|
194 |
-
["λμ€λ₯ λ°μ΄μ€ν
μμ΄ 1μ"],
|
195 |
-
["μ½μ€νΌ μμ΄ 1μ"],
|
196 |
["μ½μ€λ₯ μμ΄ 1μ"]]
|
197 |
gr.Examples(examples=examples, inputs=[input_value])
|
198 |
|
|
|
64 |
# μ΄ν μ½λ μ€ν
|
65 |
import requests
|
66 |
import gradio as gr
|
|
|
|
|
67 |
import io
|
68 |
from PIL import Image
|
69 |
from datetime import datetime, timedelta
|
70 |
from openai import OpenAI
|
71 |
|
72 |
# Perplexity AI API μ€μ
|
73 |
+
API_KEY = "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" # μ¬κΈ°μ Perplexity AI API ν€λ₯Ό μ
λ ₯νμΈμ.
|
74 |
|
75 |
# λ΄μ€ μμ½μ κ°μ Έμ€λ ν¨μ
|
76 |
def get_real_news_summary(company, date):
|
77 |
# OpenAI ν΄λΌμ΄μΈνΈ μ΄κΈ°ν
|
78 |
client = OpenAI(api_key=API_KEY, base_url="https://api.perplexity.ai")
|
79 |
+
|
80 |
# API μμ²μ μν λ©μμ§ κ΅¬μ± - νκ΅μ΄λ‘λ§ μλ΅μ λ°λλ‘ μ§μ
|
81 |
messages = [
|
82 |
{"role": "system", "content": "You are a helpful assistant that summarizes stock news strictly in Korean."},
|
83 |
{"role": "user", "content": f"Summarize the latest stock news for {company} on {date} in Korean. If there's no specific news for that date, provide the most recent relevant information in Korean."}
|
84 |
]
|
85 |
+
|
86 |
try:
|
87 |
# API μμ²
|
88 |
response = client.chat.completions.create(
|
89 |
model="llama-3.1-sonar-large-128k-online",
|
90 |
messages=messages
|
91 |
)
|
92 |
+
|
93 |
# μλ΅μμ μμ½ μΆμΆ
|
94 |
summary = response.choices[0].message.content
|
95 |
|
96 |
# νκΈ, μ«μ, 곡백, νΉμ κΈ°νΈλ§ λ¨κΈ°λ μ κ· ννμ
|
97 |
+
korean_only_summary = re.sub(r'[^\w\s#.,!%()\-\[\]]', '', summary)
|
98 |
|
99 |
# ##λ‘ μμνλ λΆλΆμ **μΌλ‘ κ°μΈμ Bold μ²λ¦¬
|
100 |
formatted_summary = re.sub(r'##\s*(.+)', r'**\1**', korean_only_summary)
|
|
|
107 |
def handle_click(input_value, date_clicked):
|
108 |
return get_real_news_summary(input_value, date_clicked)
|
109 |
|
110 |
+
# Gradioμμ μ¬μ©ν ν¨μ (λ΄μ€ μμ½ ν¬ν¨)
|
111 |
+
def update_news(input_value, selected_date):
|
112 |
+
if selected_date == "" or selected_date is None:
|
113 |
+
return "λ μ§λ₯Ό μ νν΄μ£ΌμΈμ."
|
114 |
+
else:
|
115 |
+
return handle_click(input_value, selected_date)
|
116 |
+
|
117 |
# μ’
λͺ©λͺ
κ³Ό ν°μ»€λ₯Ό λ§€ννλ λμ
λ리 νμ₯
|
118 |
name_to_ticker = {
|
119 |
"μΌμ±μ μ": "005930.KS",
|
|
|
193 |
submit_btn = gr.Button("Submit")
|
194 |
|
195 |
# μμ (μ΄μ λ μ΄μμμΌλ‘ 볡μ)
|
196 |
+
examples = [["SKλ°μ΄μ€ν"],
|
197 |
+
["λμ€λ₯ μμ΄ 1μ"],
|
198 |
+
["λμ€λ₯ μ μ½μ£Ό μμ΄ 1μ"],
|
199 |
+
["λμ€λ₯ λ°μ΄μ€ν
μμ΄ 1μ"],
|
200 |
+
["μ½μ€νΌ μμ΄ 1μ"],
|
201 |
["μ½μ€λ₯ μμ΄ 1μ"]]
|
202 |
gr.Examples(examples=examples, inputs=[input_value])
|
203 |
|