Spaces:
Paused
Paused
Download gradio_app.py from bissal/clovax-tax-chatbot: direct link, hf CLI and curl.
- Browser
- Download file 8.27 kB
-
https://huggingface.co/spaces/bissal/clovax-tax-chatbot/resolve/main/gradio_app.py
- Command line
-
hf download hf://spaces/bissal/clovax-tax-chatbot/gradio_app.py
-
curl -L -o gradio_app.py https://huggingface.co/spaces/bissal/clovax-tax-chatbot/resolve/main/gradio_app.py
8.27 kB
| # gradio_app.py - ํ ์คํธ ๊ธฐ๋ฅ์ด ํฌํจ๋ Gradio ์ฑ | |
| import gradio as gr | |
| import os | |
| import sys | |
| from datetime import datetime | |
| # ์์คํ ๋ชจ๋ import | |
| try: | |
| from tax_consultant import tax_consultant | |
| from rag_system import search_tax_law, is_rag_available, get_rag_system | |
| from space_test import gradio_test | |
| RAG_AVAILABLE = True | |
| except ImportError as e: | |
| print(f"โ ๏ธ ๋ชจ๋ import ์คํจ: {e}") | |
| RAG_AVAILABLE = False | |
| def chat_with_tax_consultant(message, history): | |
| """์ธ๋ฌด ์๋ด ์ฑ๋ด""" | |
| try: | |
| if RAG_AVAILABLE: | |
| # RAG ์์คํ ์ฌ์ฉ | |
| response = search_tax_law(message) | |
| else: | |
| # ํด๋ฐฑ ์๋ต | |
| response = f"ํ์ฌ ์์คํ ์ ๊ฒ ์ค์ ๋๋ค. ์ง๋ฌธ: {message}" | |
| return response | |
| except Exception as e: | |
| return f"์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}" | |
| def quick_test(): | |
| """๋น ๋ฅธ ์์คํ ํ ์คํธ""" | |
| try: | |
| test_results = [] | |
| # ํ๊ฒฝ ์ ๋ณด | |
| space_id = os.getenv('SPACE_ID') | |
| test_results.append(f"๐ ํ๊ฒฝ: {'ํ๊น ํ์ด์ค ์คํ์ด์ค' if space_id else '๋ก์ปฌ'}") | |
| test_results.append(f"๐ ์๊ฐ: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}") | |
| # CUDA ํ์ธ | |
| try: | |
| import torch | |
| cuda_available = torch.cuda.is_available() | |
| test_results.append(f"๐ฅ CUDA: {'์ฌ์ฉ๊ฐ๋ฅ' if cuda_available else '์ฌ์ฉ๋ถ๊ฐ'}") | |
| if cuda_available: | |
| test_results.append(f"๐ฎ GPU: {torch.cuda.get_device_name()}") | |
| except: | |
| test_results.append("โ PyTorch ๋ก๋ ์คํจ") | |
| # RAG ์์คํ ํ์ธ | |
| if RAG_AVAILABLE: | |
| try: | |
| rag = get_rag_system() | |
| test_results.append(f"โ RAG ์์คํ : ์ ์") | |
| test_results.append(f"๐ฅ๏ธ ๋๋ฐ์ด์ค: {rag.device}") | |
| if rag.vector_db: | |
| test_results.append(f"๐ ๋ฒกํฐ DB: {rag.vector_db.ntotal}๊ฐ ๋ฒกํฐ") | |
| else: | |
| test_results.append("โ ๏ธ ๋ฒกํฐ DB: ๋ฏธ๊ตฌ์ถ") | |
| except Exception as e: | |
| test_results.append(f"โ RAG ์ค๋ฅ: {str(e)}") | |
| else: | |
| test_results.append("โ RAG ์์คํ : ์ฌ์ฉ๋ถ๊ฐ") | |
| # ๊ฐ๋จํ ๊ฒ์ ํ ์คํธ | |
| if RAG_AVAILABLE: | |
| try: | |
| import time | |
| start_time = time.time() | |
| result = search_tax_law("์ทจ๋์ธ ์ธ์จ") | |
| response_time = time.time() - start_time | |
| test_results.append(f"๐ ๊ฒ์ ํ ์คํธ: {response_time:.2f}์ด") | |
| test_results.append(f"๐ ์๋ต ๊ธธ์ด: {len(result)}์") | |
| except Exception as e: | |
| test_results.append(f"โ ๊ฒ์ ํ ์คํธ ์คํจ: {str(e)}") | |
| return "\n".join(test_results) | |
| except Exception as e: | |
| return f"ํ ์คํธ ์คํจ: {str(e)}" | |
| def full_system_test(): | |
| """์ ์ฒด ์์คํ ํ ์คํธ""" | |
| try: | |
| output, results = gradio_test() | |
| return output | |
| except Exception as e: | |
| return f"์ ์ฒด ํ ์คํธ ์คํจ: {str(e)}" | |
| def sample_questions(): | |
| """์ํ ์ง๋ฌธ ์ ๊ณต""" | |
| samples = [ | |
| "์ทจ๋์ธ์จ์ด ์ผ๋ง์ธ๊ฐ์?", | |
| "1์ธ๋ 1์ฃผํ์ ๊ฐ๋ฉด ํํ์?", | |
| "์ ํผ๋ถ๋ถ ์ทจ๋์ธ ํน๋ก๋?", | |
| "๋์ง ์ทจ๋์ธ ๊ณ์ฐ๋ฐฉ๋ฒ์?", | |
| "๋ค์ฃผํ์ ์ค๊ณผ์ธ์จ์?", | |
| "์ทจ๋์ธ ์ ๊ณ ๊ธฐํ์ ์ธ์ ์ธ๊ฐ์?", | |
| "์ฆ์ฌ๋ฐ์ ๋ถ๋์ฐ์ ์ทจ๋์ธ๋?", | |
| "์์ ์ฃผํ ์ทจ๋์ธ ์ ๊ณ ๊ธฐํ์?" | |
| ] | |
| return "\n".join([f"{i+1}. {q}" for i, q in enumerate(samples)]) | |
| # Gradio ์ธํฐํ์ด์ค ๊ตฌ์ฑ | |
| def create_interface(): | |
| """Gradio ์ธํฐํ์ด์ค ์์ฑ""" | |
| with gr.Blocks(title="์ทจ๋์ธ AI ์๋ด๋ด", theme=gr.themes.Soft()) as demo: | |
| gr.Markdown(""" | |
| # ๐๏ธ ์ทจ๋์ธ AI ์๋ด๋ด | |
| ## ํ๊น ํ์ด์ค ์คํ์ด์ค์์ ์คํ๋๋ RAG ๊ธฐ๋ฐ ์ธ๋ฌด ์๋ด ์์คํ | |
| """) | |
| with gr.Tab("๐ฌ ์ธ๋ฌด ์๋ด"): | |
| with gr.Row(): | |
| with gr.Column(scale=3): | |
| chatbot = gr.Chatbot( | |
| value=[], | |
| height=400, | |
| label="์ทจ๋์ธ ์๋ด๋ด" | |
| ) | |
| msg = gr.Textbox( | |
| placeholder="์ทจ๋์ธ ๊ด๋ จ ์ง๋ฌธ์ ์ ๋ ฅํ์ธ์...", | |
| label="์ง๋ฌธ", | |
| lines=2 | |
| ) | |
| with gr.Row(): | |
| submit_btn = gr.Button("์ง๋ฌธํ๊ธฐ", variant="primary") | |
| clear_btn = gr.Button("๋ํ ์ด๊ธฐํ") | |
| with gr.Column(scale=1): | |
| gr.Markdown("### ๐ก ์ํ ์ง๋ฌธ") | |
| samples_text = gr.Textbox( | |
| value=sample_questions(), | |
| lines=10, | |
| label="ํด๋ฆญํ์ฌ ๋ณต์ฌ", | |
| interactive=False | |
| ) | |
| with gr.Tab("๐งช ์์คํ ํ ์คํธ"): | |
| with gr.Row(): | |
| with gr.Column(): | |
| gr.Markdown("### ๋น ๋ฅธ ํ ์คํธ") | |
| quick_test_btn = gr.Button("๋น ๋ฅธ ์์คํ ์ฒดํฌ", variant="secondary") | |
| quick_result = gr.Textbox( | |
| lines=10, | |
| label="ํ ์คํธ ๊ฒฐ๊ณผ", | |
| interactive=False | |
| ) | |
| with gr.Column(): | |
| gr.Markdown("### ์ ์ฒด ํ ์คํธ") | |
| full_test_btn = gr.Button("์ ์ฒด ์์คํ ํ ์คํธ", variant="primary") | |
| full_result = gr.Textbox( | |
| lines=15, | |
| label="์์ธ ํ ์คํธ ๊ฒฐ๊ณผ", | |
| interactive=False | |
| ) | |
| with gr.Tab("๐ ์์คํ ์ ๋ณด"): | |
| gr.Markdown(f""" | |
| ### ์์คํ ์ ๋ณด | |
| - **์คํ ํ๊ฒฝ**: {'ํ๊น ํ์ด์ค ์คํ์ด์ค' if os.getenv('SPACE_ID') else '๋ก์ปฌ ํ๊ฒฝ'} | |
| - **Python ๋ฒ์ **: {sys.version} | |
| - **RAG ์์คํ **: {'์ฌ์ฉ๊ฐ๋ฅ' if RAG_AVAILABLE else '์ฌ์ฉ๋ถ๊ฐ'} | |
| - **์คํ ์๊ฐ**: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')} | |
| ### ์ฌ์ฉ๋ฒ | |
| 1. **์ธ๋ฌด ์๋ด ํญ**: ์ทจ๋์ธ ๊ด๋ จ ์ง๋ฌธ์ ์ ๋ ฅํ์ธ์ | |
| 2. **์์คํ ํ ์คํธ ํญ**: ์์คํ ์ํ๋ฅผ ํ์ธํ์ธ์ | |
| 3. ์ํ ์ง๋ฌธ์ ์ฐธ๊ณ ํ์ฌ ๋ค์ํ ์ทจ๋์ธ ์ํฉ์ ๋ฌธ์ํ์ธ์ | |
| ### ์ฃผ์์ฌํญ | |
| - ๋ณธ ์์คํ ์ ์ฐธ๊ณ ์ฉ์ผ๋ก๋ง ์ฌ์ฉํ์ธ์ | |
| - ์ ํํ ์ธ๋ฌด ์๋ด์ ์ ๋ฌธ๊ฐ์ ์์ํ์๊ธฐ ๋ฐ๋๋๋ค | |
| """) | |
| # ์ด๋ฒคํธ ํธ๋ค๋ฌ | |
| def respond(message, chat_history): | |
| bot_message = chat_with_tax_consultant(message, chat_history) | |
| chat_history.append((message, bot_message)) | |
| return "", chat_history | |
| # ๋ฒํผ ์ด๋ฒคํธ ์ฐ๊ฒฐ | |
| msg.submit(respond, [msg, chatbot], [msg, chatbot]) | |
| submit_btn.click(respond, [msg, chatbot], [msg, chatbot]) | |
| clear_btn.click(lambda: [], None, chatbot) | |
| quick_test_btn.click(quick_test, None, quick_result) | |
| full_test_btn.click(full_system_test, None, full_result) | |
| return demo | |
| if __name__ == "__main__": | |
| # ํ๊ฒฝ์ ๋ฐ๋ฅธ ์ค์ | |
| space_id = os.getenv('SPACE_ID') | |
| if space_id: | |
| print(f"๐ ํ๊น ํ์ด์ค ์คํ์ด์ค์์ ์คํ: {space_id}") | |
| # ํ๊น ํ์ด์ค ์คํ์ด์ค ํ๊ฒฝ | |
| demo = create_interface() | |
| demo.launch( | |
| server_name="0.0.0.0", | |
| server_port=7860, | |
| share=False | |
| ) | |
| else: | |
| print("๐ป ๋ก์ปฌ ํ๊ฒฝ์์ ์คํ") | |
| # ๋ก์ปฌ ๊ฐ๋ฐ ํ๊ฒฝ | |
| demo = create_interface() | |
| demo.launch( | |
| server_name="127.0.0.1", | |
| server_port=7860, | |
| share=True | |
| ) |