File size: 9,320 Bytes
b14a2f9
7dde3dd
 
b14a2f9
7dde3dd
 
 
 
 
 
 
 
b14a2f9
 
7dde3dd
b14a2f9
 
 
 
 
7dde3dd
b14a2f9
7dde3dd
 
b14a2f9
7dde3dd
 
 
 
 
 
 
 
 
 
 
b14a2f9
7dde3dd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b14a2f9
7dde3dd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b14a2f9
7dde3dd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b14a2f9
 
7dde3dd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b14a2f9
 
 
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import gradio as gr
import os
import logging
from huggingface_hub import InferenceClient
from typing import List, Tuple, Generator, Dict, Any, Optional

# Configure logging
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)

class ChatConfig:
    """Configuration settings for the chat application."""
    MODEL = "google/gemma-3-27b-it"
    DEFAULT_SYSTEM_MSG = "You are a super intelligent and useful Chatbot."
    DEFAULT_MAX_TOKENS = 512
    DEFAULT_TEMP = 0.3
    DEFAULT_TOP_P = 0.95
    HF_TOKEN = os.environ.get("HF_TOKEN", None)  # Get token from environment variable

class ChatApp:
    """Main chat application class."""
    
    def __init__(self):
        """Initialize the chat application."""
        try:
            self.client = InferenceClient(
                ChatConfig.MODEL,
                token=ChatConfig.HF_TOKEN
            )
            logger.info(f"Successfully initialized InferenceClient for {ChatConfig.MODEL}")
        except Exception as e:
            logger.error(f"Failed to initialize InferenceClient: {e}")
            raise
    
    def generate_response(
        self,
        message: str,
        history: List[Tuple[str, str]],
        system_message: str = ChatConfig.DEFAULT_SYSTEM_MSG,
        max_tokens: int = ChatConfig.DEFAULT_MAX_TOKENS,
        temperature: float = ChatConfig.DEFAULT_TEMP,
        top_p: float = ChatConfig.DEFAULT_TOP_P
    ) -> Generator[str, None, None]:
        """Generate streaming responses from the model."""
        if not message.strip():
            yield "请输入消息。"
            return
            
        messages = [{"role": "system", "content": system_message}]
        
        # Add conversation history
        for user_msg, bot_msg in history:
            if user_msg:
                messages.append({"role": "user", "content": user_msg})
            if bot_msg:
                messages.append({"role": "assistant", "content": bot_msg})
        
        # Add the current message
        messages.append({"role": "user", "content": message})
        
        try:
            response = ""
            for chunk in self.client.chat_completion(
                messages,
                max_tokens=max_tokens,
                stream=True,
                temperature=temperature,
                top_p=top_p,
            ):
                token = chunk.choices[0].delta.content or ""
                response += token
                yield response
        except Exception as e:
            logger.error(f"Error generating response: {e}")
            yield f"抱歉,发生了错误: {str(e)}"
    
    def create_interface(self) -> gr.Blocks:
        """Create and configure the chat interface."""
        # Custom CSS for a modern look
        custom_css = """
        .chatbot .message {
            border-radius: 12px;
            margin: 8px;
            padding: 12px;
            box-shadow: 0 1px 3px rgba(0,0,0,0.12);
        }
        .chatbot .user-message {
            background-color: #e3f2fd;
            border-left: 4px solid #2196F3;
        }
        .chatbot .bot-message {
            background-color: #f5f5f5;
            border-left: 4px solid #9e9e9e;
        }
        .gr-button {
            border-radius: 8px;
            padding: 8px 16px;
            transition: all 0.3s ease;
        }
        .gr-button:hover {
            transform: translateY(-2px);
            box-shadow: 0 4px 8px rgba(0,0,0,0.15);
        }
        .container {
            max-width: 900px;
            margin: 0 auto;
        }
        """
        
        with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as interface:
            gr.Markdown("# 喵哥Google-Gemma-3尝鲜版")
            gr.Markdown("与Google Gemma 3 27B模型互动,可自定义参数。")
            
            with gr.Row():
                with gr.Column(scale=4):
                    chatbot = gr.Chatbot(
                        label="Gemma Chat",
                        avatar_images=("./user.png", "./botge.png"),
                        height=500,
                        show_copy_button=True,
                        elem_classes="chatbox"
                    )
                    
                    with gr.Row():
                        with gr.Column(scale=8):
                            msg = gr.Textbox(
                                show_label=False,
                                placeholder="在这里输入您的消息...",
                                container=False,
                                lines=2
                            )
                        with gr.Column(scale=1, min_width=70):
                            submit_btn = gr.Button("发送", variant="primary")
                    
                    with gr.Row():
                        clear_btn = gr.Button("清空对话", variant="secondary")
                        example_btn = gr.Button("加载示例", variant="secondary")
                
                with gr.Column(scale=2):
                    with gr.Accordion("聊天设置", open=True):
                        system_msg = gr.Textbox(
                            value=ChatConfig.DEFAULT_SYSTEM_MSG,
                            label="系统提示词",
                            lines=3,
                            placeholder="输入系统提示词..."
                        )
                        
                        with gr.Accordion("高级参数", open=False):
                            max_tokens = gr.Slider(
                                minimum=1,
                                maximum=8192,
                                value=ChatConfig.DEFAULT_MAX_TOKENS,
                                step=1,
                                label="最大标记数",
                                info="控制回复长度"
                            )
                            temperature = gr.Slider(
                                minimum=0.1,
                                maximum=1.0,
                                value=ChatConfig.DEFAULT_TEMP,
                                step=0.1,
                                label="温度",
                                info="控制随机性"
                            )
                            top_p = gr.Slider(
                                minimum=0.1,
                                maximum=1.0,
                                value=ChatConfig.DEFAULT_TOP_P,
                                step=0.05,
                                label="Top-P",
                                info="控制多样性"
                            )
                        
                        with gr.Accordion("示例问题", open=False):
                            gr.Examples(
                                examples=[
                                    ["讲一讲人工智能的最新进展"],
                                    ["写一个关于机器人发现情感的短篇故事"],
                                    ["向10岁的孩子解释量子计算"]
                                ],
                                inputs=msg
                            )
                    
                    with gr.Accordion("模型信息", open=False):
                        gr.Markdown(f"""
                        - **模型**: {ChatConfig.MODEL}
                        - **提供商**: Hugging Face
                        - **描述**: Gemma 3是Google推出的先进开源大语言模型。
                        """)
            
            # Set up event handlers
            msg_and_submit = [msg, submit_btn]
            
            submit_click = submit_btn.click(
                fn=self.generate_response,
                inputs=[msg, chatbot, system_msg, max_tokens, temperature, top_p],
                outputs=chatbot,
                api_name="chat"
            )
            
            # Submit when pressing Enter (but not when pressing Shift+Enter)
            msg.submit(
                fn=self.generate_response,
                inputs=[msg, chatbot, system_msg, max_tokens, temperature, top_p],
                outputs=chatbot,
                api_name=False
            )
            
            # Clear the textbox after sending
            submit_click.then(lambda: "", None, msg)
            msg.submit(lambda: "", None, msg)
            
            # Clear chat button
            clear_btn.click(lambda: None, None, chatbot)
            
            # Example button
            example_btn.click(
                lambda: ("介绍一下人工智能研究中最有趣的发展", []),
                None,
                [msg, chatbot]
            )
            
        return interface

def main():
    """Main function to launch the application."""
    try:
        app = ChatApp()
        interface = app.create_interface()
        
        interface.launch(
            server_name="0.0.0.0",
            server_port=7860,
            share=False,
            show_api=False,
            debug=True,
            show_error=True
        )
    except Exception as e:
        logger.critical(f"Failed to launch application: {e}")
        print(f"错误: {e}")

if __name__ == "__main__":
    main()