File size: 4,510 Bytes
526927a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/bin/env python3
"""
๊ณต๊ฐœ ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜๋Š” ๋Œ€์•ˆ ์ฝ”๋“œ
"""

import gradio as gr
import requests
import json

def chat_with_public_model(message, history):
    """๊ณต๊ฐœ ๋ชจ๋ธ๋กœ ์ฑ„ํŒ…"""
    try:
        # ๊ณต๊ฐœ ๋ชจ๋ธ ์‚ฌ์šฉ (ํ† ํฐ ๋ถˆํ•„์š”)
        api_url = "https://api-inference.huggingface.co/models/microsoft/DialoGPT-medium"
        
        data = {
            "inputs": message,
            "parameters": {
                "max_new_tokens": 100,
                "temperature": 0.7,
                "do_sample": True
            }
        }
        
        response = requests.post(api_url, json=data, timeout=30)
        
        if response.status_code == 200:
            result = response.json()
            if isinstance(result, list) and len(result) > 0:
                return result[0].get("generated_text", "์ฃ„์†กํ•ฉ๋‹ˆ๋‹ค. ์‘๋‹ต์„ ์ƒ์„ฑํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.")
            else:
                return "์ฃ„์†กํ•ฉ๋‹ˆ๋‹ค. ์‘๋‹ต์„ ์ƒ์„ฑํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."
        else:
            return f"API ํ˜ธ์ถœ ์‹คํŒจ: {response.status_code}"
            
    except Exception as e:
        return f"์˜ค๋ฅ˜ ๋ฐœ์ƒ: {str(e)}"

def solve_math_with_public_model(problem):
    """๊ณต๊ฐœ ๋ชจ๋ธ๋กœ ์ˆ˜ํ•™ ๋ฌธ์ œ ํ•ด๊ฒฐ"""
    try:
        # ์ˆ˜ํ•™ ๋ฌธ์ œ ํ•ด๊ฒฐ์„ ์œ„ํ•œ ํ”„๋กฌํ”„ํŠธ
        prompt = f"๋‹ค์Œ ์ˆ˜ํ•™ ๋ฌธ์ œ๋ฅผ ํ’€์–ด์ฃผ์„ธ์š”: {problem}"
        
        # ๊ณต๊ฐœ ๋ชจ๋ธ ์‚ฌ์šฉ
        api_url = "https://api-inference.huggingface.co/models/gpt2"
        
        data = {
            "inputs": prompt,
            "parameters": {
                "max_new_tokens": 150,
                "temperature": 0.3,
                "do_sample": True
            }
        }
        
        response = requests.post(api_url, json=data, timeout=30)
        
        if response.status_code == 200:
            result = response.json()
            if isinstance(result, list) and len(result) > 0:
                return result[0].get("generated_text", "์ฃ„์†กํ•ฉ๋‹ˆ๋‹ค. ์ˆ˜ํ•™ ๋ฌธ์ œ๋ฅผ ํ’€ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.")
            else:
                return "์ฃ„์†กํ•ฉ๋‹ˆ๋‹ค. ์ˆ˜ํ•™ ๋ฌธ์ œ๋ฅผ ํ’€ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."
        else:
            return f"API ํ˜ธ์ถœ ์‹คํŒจ: {response.status_code}"
            
    except Exception as e:
        return f"์˜ค๋ฅ˜ ๋ฐœ์ƒ: {str(e)}"

# Gradio ์ธํ„ฐํŽ˜์ด์Šค ์ƒ์„ฑ
with gr.Blocks(title="Lily Math RAG System (Public Model)", theme=gr.themes.Soft()) as demo:
    gr.Markdown("# ๐Ÿงฎ Lily Math RAG System (๊ณต๊ฐœ ๋ชจ๋ธ)")
    gr.Markdown("์ˆ˜ํ•™ ๋ฌธ์ œ ํ•ด๊ฒฐ์„ ์œ„ํ•œ AI ์‹œ์Šคํ…œ์ž…๋‹ˆ๋‹ค. (๊ณต๊ฐœ ๋ชจ๋ธ ์‚ฌ์šฉ)")
    
    with gr.Tabs():
        # ์ฑ„ํŒ… ํƒญ
        with gr.Tab("๐Ÿ’ฌ ์ฑ„ํŒ…", icon="๐Ÿ’ฌ"):
            chatbot = gr.Chatbot(height=400)
            msg = gr.Textbox(
                label="๋ฉ”์‹œ์ง€๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”",
                placeholder="์•ˆ๋…•ํ•˜์„ธ์š”! ์ˆ˜ํ•™ ๋ฌธ์ œ๋ฅผ ๋„์™€์ฃผ์„ธ์š”.",
                lines=2
            )
            clear = gr.Button("๋Œ€ํ™” ์ดˆ๊ธฐํ™”")
            
            def respond(message, chat_history):
                bot_message = chat_with_public_model(message, chat_history)
                chat_history.append((message, bot_message))
                return "", chat_history
            
            msg.submit(respond, [msg, chatbot], [msg, chatbot])
            clear.click(lambda: None, None, chatbot, queue=False)
        
        # ์ˆ˜ํ•™ ๋ฌธ์ œ ํ•ด๊ฒฐ ํƒญ
        with gr.Tab("๐Ÿงฎ ์ˆ˜ํ•™ ๋ฌธ์ œ ํ•ด๊ฒฐ", icon="๐Ÿงฎ"):
            with gr.Row():
                with gr.Column():
                    math_input = gr.Textbox(
                        label="์ˆ˜ํ•™ ๋ฌธ์ œ",
                        placeholder="์˜ˆ: 2x + 5 = 13",
                        lines=3
                    )
                    solve_btn = gr.Button("๋ฌธ์ œ ํ’€๊ธฐ", variant="primary")
                
                with gr.Column():
                    math_output = gr.Textbox(
                        label="ํ•ด๋‹ต",
                        lines=8,
                        interactive=False
                    )
            
            solve_btn.click(solve_math_with_public_model, math_input, math_output)
        
        # ์„ค์ • ํƒญ
        with gr.Tab("โš™๏ธ ์„ค์ •", icon="โš™๏ธ"):
            gr.Markdown("## ์‹œ์Šคํ…œ ์ •๋ณด")
            gr.Markdown("**๋ชจ๋ธ**: ๊ณต๊ฐœ ๋ชจ๋ธ (ํ† ํฐ ๋ถˆํ•„์š”)")
            gr.Markdown("**์ƒํƒœ**: โœ… ์ž‘๋™ ์ค‘")
            gr.Markdown("**๋ฒ„์ „**: 1.0.0 (๊ณต๊ฐœ ๋ชจ๋ธ)")

if __name__ == "__main__":
    demo.launch()