Update app-backup2.py
Browse files- app-backup2.py +88 -30
app-backup2.py
CHANGED
|
@@ -16,6 +16,8 @@ from folium.plugins import HeatMap, MarkerCluster
|
|
| 16 |
import requests
|
| 17 |
from collections import Counter
|
| 18 |
import re
|
|
|
|
|
|
|
| 19 |
|
| 20 |
# ============================================================================
|
| 21 |
# λ°μ΄ν° λ‘λ ν΄λμ€
|
|
@@ -502,12 +504,16 @@ class MarketAnalyzer:
|
|
| 502 |
class LLMQueryProcessor:
|
| 503 |
"""Fireworks AI κΈ°λ° μμ°μ΄ μ²λ¦¬"""
|
| 504 |
|
| 505 |
-
def __init__(self, api_key: str):
|
| 506 |
-
|
|
|
|
| 507 |
self.base_url = "https://api.fireworks.ai/inference/v1/chat/completions"
|
|
|
|
|
|
|
|
|
|
| 508 |
|
| 509 |
-
def process_query(self, query: str, data_context: Dict, chat_history: List = None) -> str:
|
| 510 |
-
"""μμ°μ΄ 쿼리 μ²λ¦¬"""
|
| 511 |
system_prompt = f"""λΉμ μ νκ΅ μκΆ λ°μ΄ν° λΆμ μ λ¬Έκ°μ
λλ€.
|
| 512 |
|
| 513 |
π **νμ¬ λΆμ λ°μ΄ν°**
|
|
@@ -533,14 +539,49 @@ class LLMQueryProcessor:
|
|
| 533 |
"Content-Type": "application/json"
|
| 534 |
}
|
| 535 |
|
| 536 |
-
|
| 537 |
-
|
| 538 |
-
|
| 539 |
-
|
| 540 |
-
|
| 541 |
-
|
| 542 |
-
|
| 543 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 544 |
|
| 545 |
|
| 546 |
# ============================================================================
|
|
@@ -610,16 +651,17 @@ def generate_insights():
|
|
| 610 |
return result[:11]
|
| 611 |
|
| 612 |
|
| 613 |
-
def chat_respond(message,
|
| 614 |
"""μ±λ΄ μλ΅"""
|
| 615 |
if app_state.analyzer is None:
|
| 616 |
return history + [[message, "β λ¨Όμ λ°μ΄ν°λ₯Ό λ‘λν΄μ£ΌμΈμ!"]]
|
| 617 |
|
| 618 |
data_context = app_state.analyzer.analyze_for_llm()
|
| 619 |
|
| 620 |
-
|
| 621 |
-
|
| 622 |
-
|
|
|
|
| 623 |
|
| 624 |
chat_hist = []
|
| 625 |
for user_msg, bot_msg in history:
|
|
@@ -627,8 +669,9 @@ def chat_respond(message, api_key, history):
|
|
| 627 |
chat_hist.append({"role": "assistant", "content": bot_msg})
|
| 628 |
|
| 629 |
response = app_state.llm_processor.process_query(message, data_context, chat_hist)
|
| 630 |
-
|
| 631 |
-
|
|
|
|
| 632 |
response = f"""π **κΈ°λ³Έ λ°μ΄ν° λΆμ κ²°κ³Ό**
|
| 633 |
|
| 634 |
**μ 체 νν©**
|
|
@@ -636,7 +679,13 @@ def chat_respond(message, api_key, history):
|
|
| 636 |
- μ
μ’
μ’
λ₯: {data_context['μ
μ’
_μ']}κ°
|
| 637 |
- 1μΈ΅ λΉοΏ½οΏ½: {data_context.get('1μΈ΅_λΉμ¨', 'N/A')}
|
| 638 |
|
| 639 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 640 |
|
| 641 |
history.append([message, response])
|
| 642 |
return history
|
|
@@ -658,11 +707,9 @@ with gr.Blocks(title="AI μκΆ λΆμ μμ€ν
Pro", theme=gr.themes.Soft()) a
|
|
| 658 |
with gr.Column(scale=1):
|
| 659 |
gr.Markdown("### βοΈ μ€μ ")
|
| 660 |
|
| 661 |
-
|
| 662 |
-
|
| 663 |
-
|
| 664 |
-
placeholder="API ν€λ₯Ό μ
λ ₯νμΈμ"
|
| 665 |
-
)
|
| 666 |
|
| 667 |
region_select = gr.CheckboxGroup(
|
| 668 |
choices=list(MarketDataLoader.REGIONS.keys()),
|
|
@@ -742,10 +789,10 @@ with gr.Blocks(title="AI μκΆ λΆμ μμ€ν
Pro", theme=gr.themes.Soft()) a
|
|
| 742 |
outputs=[map_output, chart1, chart2, chart3, chart4, chart5, chart6, chart7, chart8, chart9, chart10]
|
| 743 |
)
|
| 744 |
|
| 745 |
-
# μ±λ΄ μ΄λ²€νΈ
|
| 746 |
submit_btn.click(
|
| 747 |
fn=chat_respond,
|
| 748 |
-
inputs=[msg_input,
|
| 749 |
outputs=[chatbot]
|
| 750 |
).then(
|
| 751 |
fn=lambda: "",
|
|
@@ -754,7 +801,7 @@ with gr.Blocks(title="AI μκΆ λΆμ μμ€ν
Pro", theme=gr.themes.Soft()) a
|
|
| 754 |
|
| 755 |
msg_input.submit(
|
| 756 |
fn=chat_respond,
|
| 757 |
-
inputs=[msg_input,
|
| 758 |
outputs=[chatbot]
|
| 759 |
).then(
|
| 760 |
fn=lambda: "",
|
|
@@ -769,14 +816,25 @@ with gr.Blocks(title="AI μκΆ λΆμ μμ€ν
Pro", theme=gr.themes.Soft()) a
|
|
| 769 |
(sample_btn4, "νλμ°¨μ΄μ¦ μ μ μ¨?")
|
| 770 |
]:
|
| 771 |
btn.click(
|
| 772 |
-
fn=lambda t=text, h=chatbot: chat_respond(t,
|
| 773 |
outputs=[chatbot]
|
| 774 |
)
|
| 775 |
|
| 776 |
gr.Markdown("""
|
| 777 |
---
|
| 778 |
### π μ¬μ© κ°μ΄λ
|
| 779 |
-
1.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 780 |
|
| 781 |
### π μ 곡λλ 10κ°μ§ λΆμ
|
| 782 |
1. **μ
μ’
λ³ μ ν¬ μ**: κ°μ₯ λ§μ μ
μ’
TOP 15
|
|
@@ -790,7 +848,7 @@ with gr.Blocks(title="AI μκΆ λΆμ μμ€ν
Pro", theme=gr.themes.Soft()) a
|
|
| 790 |
9. **μλΆλ₯ νΈλ λ**: μΈλΆ μ
μ’
λΆν¬
|
| 791 |
10. **μ§μ νΉν**: κ° μ§μμ νΉν μ
μ’
|
| 792 |
|
| 793 |
-
π‘ **Tip**: API ν€ μμ΄λ κΈ°λ³Έ ν΅κ³λ₯Ό νμΈν μ μμ΅λλ€!
|
| 794 |
""")
|
| 795 |
|
| 796 |
# μ€ν
|
|
|
|
| 16 |
import requests
|
| 17 |
from collections import Counter
|
| 18 |
import re
|
| 19 |
+
import os
|
| 20 |
+
import time
|
| 21 |
|
| 22 |
# ============================================================================
|
| 23 |
# λ°μ΄ν° λ‘λ ν΄λμ€
|
|
|
|
| 504 |
class LLMQueryProcessor:
|
| 505 |
"""Fireworks AI κΈ°λ° μμ°μ΄ μ²λ¦¬"""
|
| 506 |
|
| 507 |
+
def __init__(self, api_key: str = None):
|
| 508 |
+
# νκ²½λ³μμμ API ν€ κ°μ Έμ€κΈ°
|
| 509 |
+
self.api_key = api_key or os.getenv("FIREWORKS_API_KEY")
|
| 510 |
self.base_url = "https://api.fireworks.ai/inference/v1/chat/completions"
|
| 511 |
+
|
| 512 |
+
if not self.api_key:
|
| 513 |
+
raise ValueError("β FIREWORKS_API_KEY νκ²½λ³μλ₯Ό μ€μ νκ±°λ API ν€λ₯Ό μ
λ ₯ν΄μ£ΌμΈμ!")
|
| 514 |
|
| 515 |
+
def process_query(self, query: str, data_context: Dict, chat_history: List = None, max_retries: int = 3) -> str:
|
| 516 |
+
"""μμ°μ΄ 쿼리 μ²λ¦¬ (μ¬μλ λ‘μ§ ν¬ν¨)"""
|
| 517 |
system_prompt = f"""λΉμ μ νκ΅ μκΆ λ°μ΄ν° λΆμ μ λ¬Έκ°μ
λλ€.
|
| 518 |
|
| 519 |
π **νμ¬ λΆμ λ°μ΄ν°**
|
|
|
|
| 539 |
"Content-Type": "application/json"
|
| 540 |
}
|
| 541 |
|
| 542 |
+
# μ¬μλ λ‘μ§
|
| 543 |
+
for attempt in range(max_retries):
|
| 544 |
+
try:
|
| 545 |
+
# νμμμμ 60μ΄λ‘ μ¦κ°
|
| 546 |
+
response = requests.post(
|
| 547 |
+
self.base_url,
|
| 548 |
+
headers=headers,
|
| 549 |
+
json=payload,
|
| 550 |
+
timeout=60
|
| 551 |
+
)
|
| 552 |
+
|
| 553 |
+
if response.status_code == 200:
|
| 554 |
+
return response.json()['choices'][0]['message']['content']
|
| 555 |
+
elif response.status_code == 429:
|
| 556 |
+
# Rate limit - μ¬μλ
|
| 557 |
+
wait_time = (attempt + 1) * 2
|
| 558 |
+
time.sleep(wait_time)
|
| 559 |
+
continue
|
| 560 |
+
else:
|
| 561 |
+
return f"β οΈ API μ€λ₯: {response.status_code} - {response.text[:200]}"
|
| 562 |
+
|
| 563 |
+
except requests.exceptions.Timeout:
|
| 564 |
+
if attempt < max_retries - 1:
|
| 565 |
+
time.sleep(2)
|
| 566 |
+
continue
|
| 567 |
+
else:
|
| 568 |
+
return "β οΈ API μλ΅ μκ° μ΄κ³Ό. μ μ ν λ€μ μλν΄μ£ΌμΈμ."
|
| 569 |
+
|
| 570 |
+
except requests.exceptions.ConnectionError:
|
| 571 |
+
if attempt < max_retries - 1:
|
| 572 |
+
time.sleep(2)
|
| 573 |
+
continue
|
| 574 |
+
else:
|
| 575 |
+
return "β οΈ λ€νΈμν¬ μ°κ²° μ€λ₯. μΈν°λ· μ°κ²°μ νμΈν΄μ£ΌμΈμ."
|
| 576 |
+
|
| 577 |
+
except Exception as e:
|
| 578 |
+
if attempt < max_retries - 1:
|
| 579 |
+
time.sleep(1)
|
| 580 |
+
continue
|
| 581 |
+
else:
|
| 582 |
+
return f"β μ€λ₯: {str(e)}"
|
| 583 |
+
|
| 584 |
+
return "β οΈ μ΅λ μ¬μλ νμ μ΄κ³Ό. μ μ ν λ€μ μλν΄μ£ΌμΈμ."
|
| 585 |
|
| 586 |
|
| 587 |
# ============================================================================
|
|
|
|
| 651 |
return result[:11]
|
| 652 |
|
| 653 |
|
| 654 |
+
def chat_respond(message, history):
|
| 655 |
"""μ±λ΄ μλ΅"""
|
| 656 |
if app_state.analyzer is None:
|
| 657 |
return history + [[message, "β λ¨Όμ λ°μ΄ν°λ₯Ό λ‘λν΄μ£ΌμΈμ!"]]
|
| 658 |
|
| 659 |
data_context = app_state.analyzer.analyze_for_llm()
|
| 660 |
|
| 661 |
+
# LLM νλ‘μΈμ μ΄κΈ°ν (νκ²½λ³μμμ API ν€ μλ λ‘λ)
|
| 662 |
+
try:
|
| 663 |
+
if app_state.llm_processor is None:
|
| 664 |
+
app_state.llm_processor = LLMQueryProcessor()
|
| 665 |
|
| 666 |
chat_hist = []
|
| 667 |
for user_msg, bot_msg in history:
|
|
|
|
| 669 |
chat_hist.append({"role": "assistant", "content": bot_msg})
|
| 670 |
|
| 671 |
response = app_state.llm_processor.process_query(message, data_context, chat_hist)
|
| 672 |
+
|
| 673 |
+
except ValueError as e:
|
| 674 |
+
# API ν€κ° μλ κ²½μ° κΈ°λ³Έ ν΅κ³ μ 곡
|
| 675 |
response = f"""π **κΈ°λ³Έ λ°μ΄ν° λΆμ κ²°κ³Ό**
|
| 676 |
|
| 677 |
**μ 체 νν©**
|
|
|
|
| 679 |
- μ
μ’
μ’
λ₯: {data_context['μ
μ’
_μ']}κ°
|
| 680 |
- 1μΈ΅ λΉοΏ½οΏ½: {data_context.get('1μΈ΅_λΉμ¨', 'N/A')}
|
| 681 |
|
| 682 |
+
β οΈ **AI λΆμ μ¬μ© λ°©λ²**
|
| 683 |
+
νκ²½λ³μλ₯Ό μ€μ νμΈμ:
|
| 684 |
+
```bash
|
| 685 |
+
export FIREWORKS_API_KEY="your_api_key_here"
|
| 686 |
+
```
|
| 687 |
+
|
| 688 |
+
λλ Hugging Face Spaceμμλ Settings > Variables μμ μ€μ νμΈμ."""
|
| 689 |
|
| 690 |
history.append([message, response])
|
| 691 |
return history
|
|
|
|
| 707 |
with gr.Column(scale=1):
|
| 708 |
gr.Markdown("### βοΈ μ€μ ")
|
| 709 |
|
| 710 |
+
# νκ²½λ³μ μν νμ
|
| 711 |
+
api_status = "β
API ν€ μ€μ λ¨" if os.getenv("FIREWORKS_API_KEY") else "β οΈ API ν€ λ―Έμ€μ (κΈ°λ³Έ ν΅κ³λ§ μ 곡)"
|
| 712 |
+
gr.Markdown(f"**π API μν**: {api_status}")
|
|
|
|
|
|
|
| 713 |
|
| 714 |
region_select = gr.CheckboxGroup(
|
| 715 |
choices=list(MarketDataLoader.REGIONS.keys()),
|
|
|
|
| 789 |
outputs=[map_output, chart1, chart2, chart3, chart4, chart5, chart6, chart7, chart8, chart9, chart10]
|
| 790 |
)
|
| 791 |
|
| 792 |
+
# μ±λ΄ μ΄λ²€νΈ (API ν€ νλΌλ―Έν° μ κ±°)
|
| 793 |
submit_btn.click(
|
| 794 |
fn=chat_respond,
|
| 795 |
+
inputs=[msg_input, chatbot],
|
| 796 |
outputs=[chatbot]
|
| 797 |
).then(
|
| 798 |
fn=lambda: "",
|
|
|
|
| 801 |
|
| 802 |
msg_input.submit(
|
| 803 |
fn=chat_respond,
|
| 804 |
+
inputs=[msg_input, chatbot],
|
| 805 |
outputs=[chatbot]
|
| 806 |
).then(
|
| 807 |
fn=lambda: "",
|
|
|
|
| 816 |
(sample_btn4, "νλμ°¨μ΄μ¦ μ μ μ¨?")
|
| 817 |
]:
|
| 818 |
btn.click(
|
| 819 |
+
fn=lambda t=text, h=chatbot: chat_respond(t, h.value or []),
|
| 820 |
outputs=[chatbot]
|
| 821 |
)
|
| 822 |
|
| 823 |
gr.Markdown("""
|
| 824 |
---
|
| 825 |
### π μ¬μ© κ°μ΄λ
|
| 826 |
+
1. μ§μ μ ν β 2. λ°μ΄ν° λ‘λ β 3. 10κ°μ§ μΈμ¬μ΄νΈ νμΈ λλ AIμκ² μ§λ¬Έ
|
| 827 |
+
|
| 828 |
+
### π AI μ±λ΄ νμ±ν λ°©λ²
|
| 829 |
+
νκ²½λ³μ μ€μ :
|
| 830 |
+
```bash
|
| 831 |
+
export FIREWORKS_API_KEY="your_api_key_here"
|
| 832 |
+
```
|
| 833 |
+
|
| 834 |
+
Hugging Face Spaceμμλ:
|
| 835 |
+
1. Settings λ©λ΄ ν΄λ¦
|
| 836 |
+
2. Variables ν μ ν
|
| 837 |
+
3. New variable μΆκ°: `FIREWORKS_API_KEY`
|
| 838 |
|
| 839 |
### π μ 곡λλ 10κ°μ§ λΆμ
|
| 840 |
1. **μ
μ’
λ³ μ ν¬ μ**: κ°μ₯ λ§μ μ
μ’
TOP 15
|
|
|
|
| 848 |
9. **μλΆλ₯ νΈλ λ**: μΈλΆ μ
μ’
λΆν¬
|
| 849 |
10. **μ§μ νΉν**: κ° μ§μμ νΉν μ
μ’
|
| 850 |
|
| 851 |
+
π‘ **Tip**: API ν€ μμ΄λ 10κ°μ§ μκ°ν λΆμκ³Ό κΈ°λ³Έ ν΅κ³λ₯Ό νμΈν μ μμ΅λλ€!
|
| 852 |
""")
|
| 853 |
|
| 854 |
# μ€ν
|