Delete app-backup.py
Browse files- app-backup.py +0 -429
app-backup.py
DELETED
|
@@ -1,429 +0,0 @@
|
|
| 1 |
-
"""
|
| 2 |
-
AI ๊ธฐ๋ฐ ์๊ถ ๋ถ์ ์์คํ
- Gradio ๋ฒ์
|
| 3 |
-
Dataset: https://huggingface.co/datasets/ginipick/market
|
| 4 |
-
"""
|
| 5 |
-
import gradio as gr
|
| 6 |
-
import pandas as pd
|
| 7 |
-
import numpy as np
|
| 8 |
-
from typing import Dict, List, Tuple
|
| 9 |
-
import json
|
| 10 |
-
from datasets import load_dataset
|
| 11 |
-
import plotly.express as px
|
| 12 |
-
import plotly.graph_objects as go
|
| 13 |
-
import folium
|
| 14 |
-
from folium.plugins import HeatMap
|
| 15 |
-
import requests
|
| 16 |
-
|
| 17 |
-
# ============================================================================
|
| 18 |
-
# ๋ฐ์ดํฐ ๋ก๋ ํด๋์ค
|
| 19 |
-
# ============================================================================
|
| 20 |
-
|
| 21 |
-
class MarketDataLoader:
|
| 22 |
-
"""ํ๊น
ํ์ด์ค ์๊ถ ๋ฐ์ดํฐ ๋ก๋"""
|
| 23 |
-
|
| 24 |
-
REGIONS = {
|
| 25 |
-
'์์ธ': '์์ธ_202506', '๊ฒฝ๊ธฐ': '๊ฒฝ๊ธฐ_202506', '๋ถ์ฐ': '๋ถ์ฐ_202506',
|
| 26 |
-
'๋๊ตฌ': '๋๊ตฌ_202506', '์ธ์ฒ': '์ธ์ฒ_202506', '๊ด์ฃผ': '๊ด์ฃผ_202506',
|
| 27 |
-
'๋์ ': '๋์ _202506', '์ธ์ฐ': '์ธ์ฐ_202506', '์ธ์ข
': '์ธ์ข
_202506',
|
| 28 |
-
'๊ฒฝ๋จ': '๊ฒฝ๋จ_202506', '๊ฒฝ๋ถ': '๊ฒฝ๋ถ_202506', '์ ๋จ': '์ ๋จ_202506',
|
| 29 |
-
'์ ๋ถ': '์ ๋ถ_202506', '์ถฉ๋จ': '์ถฉ๋จ_202506', '์ถฉ๋ถ': '์ถฉ๋ถ_202506',
|
| 30 |
-
'๊ฐ์': '๊ฐ์_202506', '์ ์ฃผ': '์ ์ฃผ_202506'
|
| 31 |
-
}
|
| 32 |
-
|
| 33 |
-
@staticmethod
|
| 34 |
-
def load_region_data(region: str, sample_size: int = 30000) -> pd.DataFrame:
|
| 35 |
-
"""์ง์ญ๋ณ ๋ฐ์ดํฐ ๋ก๋"""
|
| 36 |
-
try:
|
| 37 |
-
file_name = f"์์๊ณต์ธ์์ฅ์งํฅ๊ณต๋จ_์๊ฐ(์๊ถ)์ ๋ณด_{MarketDataLoader.REGIONS[region]}.csv"
|
| 38 |
-
dataset = load_dataset("ginipick/market", data_files=file_name, split="train")
|
| 39 |
-
df = dataset.to_pandas()
|
| 40 |
-
|
| 41 |
-
if len(df) > sample_size:
|
| 42 |
-
df = df.sample(n=sample_size, random_state=42)
|
| 43 |
-
|
| 44 |
-
return df
|
| 45 |
-
except Exception as e:
|
| 46 |
-
print(f"๋ฐ์ดํฐ ๋ก๋ ์คํจ: {str(e)}")
|
| 47 |
-
return pd.DataFrame()
|
| 48 |
-
|
| 49 |
-
@staticmethod
|
| 50 |
-
def load_multiple_regions(regions: List[str], sample_per_region: int = 30000) -> pd.DataFrame:
|
| 51 |
-
"""์ฌ๋ฌ ์ง์ญ ๋ฐ์ดํฐ ๋ก๋"""
|
| 52 |
-
dfs = []
|
| 53 |
-
for region in regions:
|
| 54 |
-
df = MarketDataLoader.load_region_data(region, sample_per_region)
|
| 55 |
-
if not df.empty:
|
| 56 |
-
dfs.append(df)
|
| 57 |
-
|
| 58 |
-
if dfs:
|
| 59 |
-
return pd.concat(dfs, ignore_index=True)
|
| 60 |
-
return pd.DataFrame()
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
# ============================================================================
|
| 64 |
-
# ์๊ถ ๋ถ์ ํด๋์ค
|
| 65 |
-
# ============================================================================
|
| 66 |
-
|
| 67 |
-
class MarketAnalyzer:
|
| 68 |
-
"""์๊ถ ๋ฐ์ดํฐ ๋ถ์ ์์ง"""
|
| 69 |
-
|
| 70 |
-
def __init__(self, df: pd.DataFrame):
|
| 71 |
-
self.df = df
|
| 72 |
-
self.prepare_data()
|
| 73 |
-
|
| 74 |
-
def prepare_data(self):
|
| 75 |
-
"""๋ฐ์ดํฐ ์ ์ฒ๋ฆฌ"""
|
| 76 |
-
if '๊ฒฝ๋' in self.df.columns:
|
| 77 |
-
self.df['๊ฒฝ๋'] = pd.to_numeric(self.df['๊ฒฝ๋'], errors='coerce')
|
| 78 |
-
if '์๋' in self.df.columns:
|
| 79 |
-
self.df['์๋'] = pd.to_numeric(self.df['์๋'], errors='coerce')
|
| 80 |
-
self.df = self.df.dropna(subset=['๊ฒฝ๋', '์๋'])
|
| 81 |
-
|
| 82 |
-
def get_top_insights(self, n: int = 10) -> List[Dict]:
|
| 83 |
-
"""์์ ์ธ์ฌ์ดํธ ์ถ์ถ"""
|
| 84 |
-
insights = []
|
| 85 |
-
|
| 86 |
-
# 1. ์
์ข
๋ณ ์ ํฌ ์
|
| 87 |
-
if '์๊ถ์
์ข
์ค๋ถ๋ฅ๋ช
' in self.df.columns:
|
| 88 |
-
top_categories = self.df['์๊ถ์
์ข
์ค๋ถ๋ฅ๋ช
'].value_counts().head(n)
|
| 89 |
-
fig = px.bar(x=top_categories.index, y=top_categories.values,
|
| 90 |
-
labels={'x': '์
์ข
', 'y': '์ ํฌ ์'},
|
| 91 |
-
title='๐ ์์ ์
์ข
Top 10')
|
| 92 |
-
insights.append(('bar', fig, f"์ ์ฒด {len(self.df):,}๊ฐ ์ ํฌ ์ค ์์ ์
์ข
"))
|
| 93 |
-
|
| 94 |
-
# 2. ์ง์ญ๋ณ ๋ถํฌ
|
| 95 |
-
if '์๋๋ช
' in self.df.columns:
|
| 96 |
-
region_counts = self.df['์๋๋ช
'].value_counts()
|
| 97 |
-
fig = px.pie(values=region_counts.values, names=region_counts.index,
|
| 98 |
-
title='๐ ์ง์ญ๋ณ ์ ํฌ ๋ถํฌ', hole=0.3)
|
| 99 |
-
insights.append(('pie', fig, '์ ๊ตญ ์ง์ญ๋ณ ์๊ถ ๋ถํฌ๋'))
|
| 100 |
-
|
| 101 |
-
# 3. ์๊ตฐ๊ตฌ๋ณ
|
| 102 |
-
if '์๊ตฐ๊ตฌ๋ช
' in self.df.columns:
|
| 103 |
-
district_counts = self.df['์๊ตฐ๊ตฌ๋ช
'].value_counts().head(15)
|
| 104 |
-
fig = px.bar(x=district_counts.index, y=district_counts.values,
|
| 105 |
-
labels={'x': '์๊ตฐ๊ตฌ', 'y': '์ ํฌ ์'},
|
| 106 |
-
title='๐ ์๊ตฐ๊ตฌ๋ณ ์๊ถ ๋ฐ์ง๋')
|
| 107 |
-
insights.append(('bar', fig, '์ ํฌ๊ฐ ๊ฐ์ฅ ๋ง์ ์๊ตฐ๊ตฌ'))
|
| 108 |
-
|
| 109 |
-
# 4. ์ธต๋ณ ๋ถํฌ
|
| 110 |
-
if '์ธต์ ๋ณด' in self.df.columns:
|
| 111 |
-
floor_counts = self.df['์ธต์ ๋ณด'].value_counts().head(10)
|
| 112 |
-
fig = px.bar(x=floor_counts.index, y=floor_counts.values,
|
| 113 |
-
labels={'x': '์ธต', 'y': '์ ํฌ ์'},
|
| 114 |
-
title='๐ข ์ธต๋ณ ์ ํฌ ๋ถํฌ')
|
| 115 |
-
insights.append(('bar', fig, '1์ธต์ด ๊ฐ์ฅ ์ ํธ๋๋ ์ธต'))
|
| 116 |
-
|
| 117 |
-
# 5. ํ๋์ฐจ์ด์ฆ
|
| 118 |
-
if '์ํธ๋ช
' in self.df.columns:
|
| 119 |
-
franchise_keywords = ['์คํ๋ฒ
์ค', 'CU', 'GS25', '์ธ๋ธ์ผ๋ ๋ธ', '์ด๋งํธ24',
|
| 120 |
-
'๋งฅ๋๋ ๋', 'BBQ', '๊ต์ด', '๋ฒ๊ฑฐํน', '๋กฏ๋ฐ๋ฆฌ์']
|
| 121 |
-
franchise_data = {}
|
| 122 |
-
for keyword in franchise_keywords:
|
| 123 |
-
count = self.df['์ํธ๋ช
'].str.contains(keyword, case=False, na=False).sum()
|
| 124 |
-
if count > 0:
|
| 125 |
-
franchise_data[keyword] = count
|
| 126 |
-
|
| 127 |
-
if franchise_data:
|
| 128 |
-
fig = px.bar(x=list(franchise_data.keys()), y=list(franchise_data.values()),
|
| 129 |
-
labels={'x': '๋ธ๋๋', 'y': '์ ํฌ ์'},
|
| 130 |
-
title='๐ช ์ฃผ์ ํ๋์ฐจ์ด์ฆ ์ ํฌ ์')
|
| 131 |
-
insights.append(('bar', fig, '์ ๊ตญ ์ฃผ์ ํ๋์ฐจ์ด์ฆ ๋ถํฌ'))
|
| 132 |
-
|
| 133 |
-
return insights[:5] # ์์ 5๊ฐ๋ง
|
| 134 |
-
|
| 135 |
-
def create_density_map(self, sample_size: int = 1000) -> str:
|
| 136 |
-
"""์ ํฌ ๋ฐ์ง๋ ์ง๋ ์์ฑ (HTML ๋ฐํ)"""
|
| 137 |
-
df_sample = self.df.sample(n=min(sample_size, len(self.df)), random_state=42)
|
| 138 |
-
|
| 139 |
-
center_lat = df_sample['์๋'].mean()
|
| 140 |
-
center_lon = df_sample['๊ฒฝ๋'].mean()
|
| 141 |
-
|
| 142 |
-
m = folium.Map(location=[center_lat, center_lon], zoom_start=11, tiles='OpenStreetMap')
|
| 143 |
-
heat_data = [[row['์๋'], row['๊ฒฝ๋']] for _, row in df_sample.iterrows()]
|
| 144 |
-
HeatMap(heat_data, radius=15, blur=25, max_zoom=13).add_to(m)
|
| 145 |
-
|
| 146 |
-
return m._repr_html_()
|
| 147 |
-
|
| 148 |
-
def analyze_for_llm(self) -> Dict:
|
| 149 |
-
"""LLM ์ปจํ
์คํธ์ฉ ๋ถ์ ๋ฐ์ดํฐ"""
|
| 150 |
-
context = {
|
| 151 |
-
'์ด_์ ํฌ_์': len(self.df),
|
| 152 |
-
'์ง์ญ_์': self.df['์๋๋ช
'].nunique() if '์๋๋ช
' in self.df.columns else 0,
|
| 153 |
-
'์
์ข
_์': self.df['์๊ถ์
์ข
์ค๋ถ๋ฅ๋ช
'].nunique() if '์๊ถ์
์ข
์ค๋ถ๋ฅ๋ช
' in self.df.columns else 0,
|
| 154 |
-
}
|
| 155 |
-
|
| 156 |
-
if '์๊ถ์
์ข
์ค๋ถ๋ฅ๋ช
' in self.df.columns:
|
| 157 |
-
context['์์_์
์ข
_5'] = self.df['์๊ถ์
์ข
์ค๋ถ๋ฅ๋ช
'].value_counts().head(5).to_dict()
|
| 158 |
-
|
| 159 |
-
return context
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
# ============================================================================
|
| 163 |
-
# LLM ์ฟผ๋ฆฌ ํ๋ก์ธ์
|
| 164 |
-
# ============================================================================
|
| 165 |
-
|
| 166 |
-
class LLMQueryProcessor:
|
| 167 |
-
"""Fireworks AI ๊ธฐ๋ฐ ์์ฐ์ด ์ฒ๋ฆฌ"""
|
| 168 |
-
|
| 169 |
-
def __init__(self, api_key: str):
|
| 170 |
-
self.api_key = api_key
|
| 171 |
-
self.base_url = "https://api.fireworks.ai/inference/v1/chat/completions"
|
| 172 |
-
|
| 173 |
-
def process_query(self, query: str, data_context: Dict, chat_history: List = None) -> str:
|
| 174 |
-
"""์์ฐ์ด ์ฟผ๋ฆฌ ์ฒ๋ฆฌ"""
|
| 175 |
-
system_prompt = f"""๋น์ ์ ํ๊ตญ ์๊ถ ๋ฐ์ดํฐ ๋ถ์ ์ ๋ฌธ๊ฐ์
๋๋ค.
|
| 176 |
-
|
| 177 |
-
๐ **ํ์ฌ ๋ถ์ ๋ฐ์ดํฐ**
|
| 178 |
-
{json.dumps(data_context, ensure_ascii=False, indent=2)}
|
| 179 |
-
|
| 180 |
-
๊ตฌ์ฒด์ ์ธ ์ซ์์ ๋น์จ๋ก ์ ๋์ ๋ถ์์ ์ ๊ณตํ์ธ์.
|
| 181 |
-
์ฐฝ์
, ํฌ์, ๊ฒฝ์ ๋ถ์ ๊ด์ ์์ ์ค์ฉ์ ์ธ์ฌ์ดํธ๋ฅผ ์ ๊ณตํ์ธ์."""
|
| 182 |
-
|
| 183 |
-
messages = [{"role": "system", "content": system_prompt}]
|
| 184 |
-
if chat_history:
|
| 185 |
-
messages.extend(chat_history[-6:])
|
| 186 |
-
messages.append({"role": "user", "content": query})
|
| 187 |
-
|
| 188 |
-
payload = {
|
| 189 |
-
"model": "accounts/fireworks/models/qwen3-235b-a22b-instruct-2507",
|
| 190 |
-
"max_tokens": 2000,
|
| 191 |
-
"temperature": 0.7,
|
| 192 |
-
"messages": messages
|
| 193 |
-
}
|
| 194 |
-
|
| 195 |
-
headers = {
|
| 196 |
-
"Authorization": f"Bearer {self.api_key}",
|
| 197 |
-
"Content-Type": "application/json"
|
| 198 |
-
}
|
| 199 |
-
|
| 200 |
-
try:
|
| 201 |
-
response = requests.post(self.base_url, headers=headers, json=payload, timeout=30)
|
| 202 |
-
if response.status_code == 200:
|
| 203 |
-
return response.json()['choices'][0]['message']['content']
|
| 204 |
-
else:
|
| 205 |
-
return f"โ ๏ธ API ์ค๋ฅ: {response.status_code}"
|
| 206 |
-
except Exception as e:
|
| 207 |
-
return f"โ ์ค๋ฅ: {str(e)}"
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
# ============================================================================
|
| 211 |
-
# ์ ์ญ ์ํ
|
| 212 |
-
# ============================================================================
|
| 213 |
-
|
| 214 |
-
class AppState:
|
| 215 |
-
def __init__(self):
|
| 216 |
-
self.analyzer = None
|
| 217 |
-
self.llm_processor = None
|
| 218 |
-
self.chat_history = []
|
| 219 |
-
|
| 220 |
-
app_state = AppState()
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
# ============================================================================
|
| 224 |
-
# Gradio ์ธํฐํ์ด์ค ํจ์
|
| 225 |
-
# ============================================================================
|
| 226 |
-
|
| 227 |
-
def load_data(regions):
|
| 228 |
-
"""๋ฐ์ดํฐ ๋ก๋"""
|
| 229 |
-
if not regions:
|
| 230 |
-
return "โ ์ต์ 1๊ฐ ์ง์ญ์ ์ ํํด์ฃผ์ธ์!", None, None, None
|
| 231 |
-
|
| 232 |
-
try:
|
| 233 |
-
df = MarketDataLoader.load_multiple_regions(regions, sample_per_region=30000)
|
| 234 |
-
if df.empty:
|
| 235 |
-
return "โ ๋ฐ์ดํฐ ๋ก๋ ์คํจ!", None, None, None
|
| 236 |
-
|
| 237 |
-
app_state.analyzer = MarketAnalyzer(df)
|
| 238 |
-
|
| 239 |
-
# ๊ธฐ๋ณธ ํต๊ณ
|
| 240 |
-
stats = f"""
|
| 241 |
-
โ
**๋ฐ์ดํฐ ๋ก๋ ์๋ฃ!**
|
| 242 |
-
|
| 243 |
-
๐ **ํต๊ณ**
|
| 244 |
-
- ์ด ์ ํฌ: {len(df):,}๊ฐ
|
| 245 |
-
- ๋ถ์ ์ง์ญ: {', '.join(regions)}
|
| 246 |
-
- ์
์ข
์: {df['์๊ถ์
์ข
์ค๋ถ๋ฅ๋ช
'].nunique()}๊ฐ
|
| 247 |
-
"""
|
| 248 |
-
|
| 249 |
-
return stats, gr.update(visible=True), gr.update(visible=True), gr.update(visible=True)
|
| 250 |
-
except Exception as e:
|
| 251 |
-
return f"โ ์ค๋ฅ: {str(e)}", None, None, None
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
def generate_insights():
|
| 255 |
-
"""์ธ์ฌ์ดํธ ์์ฑ"""
|
| 256 |
-
if app_state.analyzer is None:
|
| 257 |
-
return [None] * 6
|
| 258 |
-
|
| 259 |
-
insights = app_state.analyzer.get_top_insights(5)
|
| 260 |
-
map_html = app_state.analyzer.create_density_map(sample_size=2000)
|
| 261 |
-
|
| 262 |
-
result = [map_html]
|
| 263 |
-
for _, fig, _ in insights:
|
| 264 |
-
result.append(fig)
|
| 265 |
-
|
| 266 |
-
# ๋ถ์กฑํ ์ฐจํธ๋ None์ผ๋ก ์ฑ์ฐ๊ธฐ
|
| 267 |
-
while len(result) < 6:
|
| 268 |
-
result.append(None)
|
| 269 |
-
|
| 270 |
-
return result[:6]
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
def chat_respond(message, api_key, history):
|
| 274 |
-
"""์ฑ๋ด ์๋ต"""
|
| 275 |
-
if app_state.analyzer is None:
|
| 276 |
-
return history + [[message, "โ ๋จผ์ ๋ฐ์ดํฐ๋ฅผ ๋ก๋ํด์ฃผ์ธ์!"]]
|
| 277 |
-
|
| 278 |
-
data_context = app_state.analyzer.analyze_for_llm()
|
| 279 |
-
|
| 280 |
-
if api_key:
|
| 281 |
-
if app_state.llm_processor is None or app_state.llm_processor.api_key != api_key:
|
| 282 |
-
app_state.llm_processor = LLMQueryProcessor(api_key)
|
| 283 |
-
|
| 284 |
-
chat_hist = []
|
| 285 |
-
for user_msg, bot_msg in history:
|
| 286 |
-
chat_hist.append({"role": "user", "content": user_msg})
|
| 287 |
-
chat_hist.append({"role": "assistant", "content": bot_msg})
|
| 288 |
-
|
| 289 |
-
response = app_state.llm_processor.process_query(message, data_context, chat_hist)
|
| 290 |
-
else:
|
| 291 |
-
# ๊ธฐ๋ณธ ํต๊ณ
|
| 292 |
-
response = f"""๐ **๊ธฐ๋ณธ ๋ฐ์ดํฐ ๋ถ์ ๊ฒฐ๊ณผ**
|
| 293 |
-
|
| 294 |
-
**์ ์ฒด ํํฉ**
|
| 295 |
-
- ์ด ์ ํฌ ์: {data_context['์ด_์ ํฌ_์']:,}๊ฐ
|
| 296 |
-
- ์
์ข
์ข
๋ฅ: {data_context['์
์ข
_์']}๊ฐ
|
| 297 |
-
|
| 298 |
-
๐ก API ํค๋ฅผ ์
๋ ฅํ๋ฉด ๋ ์์ธํ AI ๋ถ์์ ๋ฐ์ ์ ์์ต๋๋ค!"""
|
| 299 |
-
|
| 300 |
-
history.append([message, response])
|
| 301 |
-
return history
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
# ============================================================================
|
| 305 |
-
# Gradio UI
|
| 306 |
-
# ============================================================================
|
| 307 |
-
|
| 308 |
-
with gr.Blocks(title="AI ์๊ถ ๋ถ์ ์์คํ
", theme=gr.themes.Soft()) as demo:
|
| 309 |
-
gr.Markdown("""
|
| 310 |
-
# ๐ช AI ์๊ถ ๋ถ์ ์์คํ
|
| 311 |
-
*์ ๊ตญ ์๊ฐ(์๊ถ) ๋ฐ์ดํฐ ๊ธฐ๋ฐ ์ค์๊ฐ ๋ถ์ | Powered by Fireworks AI*
|
| 312 |
-
""")
|
| 313 |
-
|
| 314 |
-
with gr.Row():
|
| 315 |
-
with gr.Column(scale=1):
|
| 316 |
-
gr.Markdown("### โ๏ธ ์ค์ ")
|
| 317 |
-
|
| 318 |
-
api_key_input = gr.Textbox(
|
| 319 |
-
label="๐ Fireworks AI API Key (์ ํ)",
|
| 320 |
-
type="password",
|
| 321 |
-
placeholder="API ํค๋ฅผ ์
๋ ฅํ์ธ์"
|
| 322 |
-
)
|
| 323 |
-
|
| 324 |
-
region_select = gr.CheckboxGroup(
|
| 325 |
-
choices=list(MarketDataLoader.REGIONS.keys()),
|
| 326 |
-
value=['์์ธ'],
|
| 327 |
-
label="๐ ๋ถ์ ์ง์ญ ์ ํ (์ต๋ 5๊ฐ ๊ถ์ฅ)"
|
| 328 |
-
)
|
| 329 |
-
|
| 330 |
-
load_btn = gr.Button("๐ ๋ฐ์ดํฐ ๋ก๋", variant="primary", size="lg")
|
| 331 |
-
|
| 332 |
-
status_box = gr.Markdown("๐ ์ง์ญ์ ์ ํํ๊ณ ๋ฐ์ดํฐ๋ฅผ ๋ก๋ํ์ธ์!")
|
| 333 |
-
|
| 334 |
-
with gr.Column(scale=3):
|
| 335 |
-
with gr.Tabs() as tabs:
|
| 336 |
-
with gr.Tab("๐ ์ธ์ฌ์ดํธ ๋์๋ณด๋", id=0) as tab1:
|
| 337 |
-
insights_content = gr.Column(visible=False)
|
| 338 |
-
|
| 339 |
-
with insights_content:
|
| 340 |
-
gr.Markdown("### ๐บ๏ธ ์ ํฌ ๋ฐ์ง๋ ํํธ๋งต")
|
| 341 |
-
map_output = gr.HTML()
|
| 342 |
-
|
| 343 |
-
gr.Markdown("---")
|
| 344 |
-
gr.Markdown("### ๐ ์๊ถ ์ธ์ฌ์ดํธ")
|
| 345 |
-
|
| 346 |
-
chart1 = gr.Plot(label="์ธ์ฌ์ดํธ 1")
|
| 347 |
-
chart2 = gr.Plot(label="์ธ์ฌ์ดํธ 2")
|
| 348 |
-
chart3 = gr.Plot(label="์ธ์ฌ์ดํธ 3")
|
| 349 |
-
chart4 = gr.Plot(label="์ธ์ฌ์ดํธ 4")
|
| 350 |
-
chart5 = gr.Plot(label="์ธ์ฌ์ดํธ 5")
|
| 351 |
-
|
| 352 |
-
with gr.Tab("๐ค AI ๋ถ์ ์ฑ๋ด", id=1) as tab2:
|
| 353 |
-
chat_content = gr.Column(visible=False)
|
| 354 |
-
|
| 355 |
-
with chat_content:
|
| 356 |
-
gr.Markdown("""
|
| 357 |
-
### ๐ก ์ํ ์ง๋ฌธ
|
| 358 |
-
๊ฐ๋จ์์ ์นดํ ์ฐฝ์
? | ์นํจ์ง ํฌํ ์ง์ญ? | 1์ธต์ด ์ ๋ฆฌํ ์
์ข
? | ํ๋์ฐจ์ด์ฆ ์ ์ ์จ?
|
| 359 |
-
""")
|
| 360 |
-
|
| 361 |
-
chatbot = gr.Chatbot(height=400, label="AI ์๊ถ ๋ถ์ ์ด์์คํดํธ")
|
| 362 |
-
|
| 363 |
-
with gr.Row():
|
| 364 |
-
msg_input = gr.Textbox(
|
| 365 |
-
placeholder="๋ฌด์์ด๋ ๋ฌผ์ด๋ณด์ธ์! (์: ๊ฐ๋จ์์ ์นดํ ์ฐฝ์
ํ๋ ค๋ฉด?)",
|
| 366 |
-
show_label=False,
|
| 367 |
-
scale=4
|
| 368 |
-
)
|
| 369 |
-
submit_btn = gr.Button("์ ์ก", variant="primary", scale=1)
|
| 370 |
-
|
| 371 |
-
# ์ํ ๋ฒํผ๋ค
|
| 372 |
-
with gr.Row():
|
| 373 |
-
sample_btn1 = gr.Button("๊ฐ๋จ์์ ์นดํ ์ฐฝ์
?", size="sm")
|
| 374 |
-
sample_btn2 = gr.Button("์นํจ์ง ํฌํ ์ง์ญ?", size="sm")
|
| 375 |
-
sample_btn3 = gr.Button("1์ธต์ด ์ ๋ฆฌํ ์
์ข
?", size="sm")
|
| 376 |
-
sample_btn4 = gr.Button("ํ๋์ฐจ์ด์ฆ ์ ์ ์จ?", size="sm")
|
| 377 |
-
|
| 378 |
-
# ์ด๋ฒคํธ ํธ๋ค๋ฌ
|
| 379 |
-
load_btn.click(
|
| 380 |
-
fn=load_data,
|
| 381 |
-
inputs=[region_select],
|
| 382 |
-
outputs=[status_box, insights_content, chat_content, tab1]
|
| 383 |
-
).then(
|
| 384 |
-
fn=generate_insights,
|
| 385 |
-
outputs=[map_output, chart1, chart2, chart3, chart4, chart5]
|
| 386 |
-
)
|
| 387 |
-
|
| 388 |
-
# ์ฑ๋ด ์ด๋ฒคํธ
|
| 389 |
-
submit_btn.click(
|
| 390 |
-
fn=chat_respond,
|
| 391 |
-
inputs=[msg_input, api_key_input, chatbot],
|
| 392 |
-
outputs=[chatbot]
|
| 393 |
-
).then(
|
| 394 |
-
fn=lambda: "",
|
| 395 |
-
outputs=[msg_input]
|
| 396 |
-
)
|
| 397 |
-
|
| 398 |
-
msg_input.submit(
|
| 399 |
-
fn=chat_respond,
|
| 400 |
-
inputs=[msg_input, api_key_input, chatbot],
|
| 401 |
-
outputs=[chatbot]
|
| 402 |
-
).then(
|
| 403 |
-
fn=lambda: "",
|
| 404 |
-
outputs=[msg_input]
|
| 405 |
-
)
|
| 406 |
-
|
| 407 |
-
# ์ํ ๋ฒํผ ์ด๋ฒคํธ
|
| 408 |
-
for btn, text in [
|
| 409 |
-
(sample_btn1, "๊ฐ๋จ์์ ์นดํ ์ฐฝ์
?"),
|
| 410 |
-
(sample_btn2, "์นํจ์ง ํฌํ ์ง์ญ?"),
|
| 411 |
-
(sample_btn3, "1์ธต์ด ์ ๋ฆฌํ ์
์ข
?"),
|
| 412 |
-
(sample_btn4, "ํ๋์ฐจ์ด์ฆ ์ ์ ์จ?")
|
| 413 |
-
]:
|
| 414 |
-
btn.click(
|
| 415 |
-
fn=lambda t=text, h=chatbot: chat_respond(t, api_key_input.value, h.value or []),
|
| 416 |
-
outputs=[chatbot]
|
| 417 |
-
)
|
| 418 |
-
|
| 419 |
-
gr.Markdown("""
|
| 420 |
-
---
|
| 421 |
-
### ๐ ์ฌ์ฉ ๊ฐ์ด๋
|
| 422 |
-
1. API ํค ์
๋ ฅ (์ ํ) โ 2. ์ง์ญ ์ ํ โ 3. ๋ฐ์ดํฐ ๋ก๋ โ 4. ์ธ์ฌ์ดํธ ํ์ธ ๋๋ AI์๊ฒ ์ง๋ฌธ
|
| 423 |
-
|
| 424 |
-
๐ก **Tip**: API ํค ์์ด๋ ๊ธฐ๋ณธ ํต๊ณ๋ฅผ ํ์ธํ ์ ์์ต๋๋ค!
|
| 425 |
-
""")
|
| 426 |
-
|
| 427 |
-
# ์คํ
|
| 428 |
-
if __name__ == "__main__":
|
| 429 |
-
demo.launch(server_name="0.0.0.0", server_port=7860, share=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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