miaoge commited on
Commit
7dde3dd
·
verified ·
1 Parent(s): ca2bc1e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +228 -113
app.py CHANGED
@@ -1,132 +1,247 @@
1
  import gradio as gr
 
 
2
  from huggingface_hub import InferenceClient
3
- from typing import List, Tuple
 
 
 
 
 
 
 
4
 
5
- # Default settings
6
  class ChatConfig:
 
7
  MODEL = "google/gemma-3-27b-it"
8
  DEFAULT_SYSTEM_MSG = "You are a super intelligent and useful Chatbot."
9
  DEFAULT_MAX_TOKENS = 512
10
  DEFAULT_TEMP = 0.3
11
  DEFAULT_TOP_P = 0.95
 
12
 
13
- client = InferenceClient(ChatConfig.MODEL)
14
-
15
- def generate_response(
16
- message: str,
17
- history: List[Tuple[str, str]],
18
- system_message: str = ChatConfig.DEFAULT_SYSTEM_MSG,
19
- max_tokens: int = ChatConfig.DEFAULT_MAX_TOKENS,
20
- temperature: float = ChatConfig.DEFAULT_TEMP,
21
- top_p: float = ChatConfig.DEFAULT_TOP_P
22
- ) -> str:
23
- messages = [{"role": "system", "content": system_message}]
24
 
25
- # Conversation history
26
- for user_msg, bot_msg in history:
27
- if user_msg:
28
- messages.append({"role": "user", "content": user_msg})
29
- if bot_msg:
30
- messages.append({"role": "assistant", "content": bot_msg})
 
 
 
 
 
31
 
32
- messages.append({"role": "user", "content": message})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
 
34
- response = ""
35
- for chunk in client.chat_completion(
36
- messages,
37
- max_tokens=max_tokens,
38
- stream=True,
39
- temperature=temperature,
40
- top_p=top_p,
41
- ):
42
- token = chunk.choices[0].delta.content or ""
43
- response += token
44
- yield response
45
-
46
-
47
- def create_interface() -> gr.ChatInterface:
48
- """Create and configure the chat interface."""
49
- # Custom CSS for a modern look
50
- custom_css = """
51
- .chatbot .message {
52
- border-radius: 12px;
53
- margin: 5px;
54
- padding: 10px;
55
- }
56
- .chatbot .user-message {
57
- background-color: #e3f2fd;
58
- }
59
- .chatbot .bot-message {
60
- background-color: #f5f5f5;
61
- }
62
- .gr-button {
63
- border-radius: 8px;
64
- padding: 8px 16px;
65
- }
66
- """
67
-
68
- # Custom chatbot
69
- chatbot = gr.Chatbot(
70
- label="Gemma Chat",
71
- avatar_images=("./user.png", "./botge.png"),
72
- height=450,
73
- show_copy_button=True
74
- )
75
-
76
- # Chat interface
77
- interface = gr.ChatInterface(
78
- fn=generate_response,
79
- chatbot=chatbot,
80
- title="Tomoniai's chat with Google-Gemma-3",
81
- theme=gr.themes.Soft(),
82
- css=custom_css,
83
- additional_inputs=[
84
- gr.Textbox(
85
- value=ChatConfig.DEFAULT_SYSTEM_MSG,
86
- label="System Prompt",
87
- lines=2,
88
- placeholder="Enter system message..."
89
- ),
90
- gr.Slider(
91
- minimum=1,
92
- maximum=8192,
93
- value=ChatConfig.DEFAULT_MAX_TOKENS,
94
- step=1,
95
- label="Max Tokens",
96
- info="Controls response length"
97
- ),
98
- gr.Slider(
99
- minimum=0.1,
100
- maximum=1.0,
101
- value=ChatConfig.DEFAULT_TEMP,
102
- step=0.1,
103
- label="Temperature",
104
- info="Controls randomness"
105
- ),
106
- gr.Slider(
107
- minimum=0.1,
108
- maximum=1.0,
109
- value=ChatConfig.DEFAULT_TOP_P,
110
- step=0.05,
111
- label="Top-P",
112
- info="Controls diversity"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
113
  )
114
- ],
115
- additional_inputs_accordion=gr.Accordion(label="Advanced Settings", open=False)
116
- )
117
-
118
- return interface
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
119
 
120
  def main():
121
- app = create_interface()
122
- app.launch(
123
- server_name="0.0.0.0",
124
- server_port=7860,
125
- share=False,
126
- show_api=False,
127
- show_error=True,
128
- debug=True
129
- )
 
 
 
 
 
 
 
130
 
131
  if __name__ == "__main__":
132
  main()
 
1
  import gradio as gr
2
+ import os
3
+ import logging
4
  from huggingface_hub import InferenceClient
5
+ from typing import List, Tuple, Generator, Dict, Any, Optional
6
+
7
+ # Configure logging
8
+ logging.basicConfig(
9
+ level=logging.INFO,
10
+ format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
11
+ )
12
+ logger = logging.getLogger(__name__)
13
 
 
14
  class ChatConfig:
15
+ """Configuration settings for the chat application."""
16
  MODEL = "google/gemma-3-27b-it"
17
  DEFAULT_SYSTEM_MSG = "You are a super intelligent and useful Chatbot."
18
  DEFAULT_MAX_TOKENS = 512
19
  DEFAULT_TEMP = 0.3
20
  DEFAULT_TOP_P = 0.95
21
+ HF_TOKEN = os.environ.get("HF_TOKEN", None) # Get token from environment variable
22
 
23
+ class ChatApp:
24
+ """Main chat application class."""
 
 
 
 
 
 
 
 
 
25
 
26
+ def __init__(self):
27
+ """Initialize the chat application."""
28
+ try:
29
+ self.client = InferenceClient(
30
+ ChatConfig.MODEL,
31
+ token=ChatConfig.HF_TOKEN
32
+ )
33
+ logger.info(f"Successfully initialized InferenceClient for {ChatConfig.MODEL}")
34
+ except Exception as e:
35
+ logger.error(f"Failed to initialize InferenceClient: {e}")
36
+ raise
37
 
38
+ def generate_response(
39
+ self,
40
+ message: str,
41
+ history: List[Tuple[str, str]],
42
+ system_message: str = ChatConfig.DEFAULT_SYSTEM_MSG,
43
+ max_tokens: int = ChatConfig.DEFAULT_MAX_TOKENS,
44
+ temperature: float = ChatConfig.DEFAULT_TEMP,
45
+ top_p: float = ChatConfig.DEFAULT_TOP_P
46
+ ) -> Generator[str, None, None]:
47
+ """Generate streaming responses from the model."""
48
+ if not message.strip():
49
+ yield "请输入消息。"
50
+ return
51
+
52
+ messages = [{"role": "system", "content": system_message}]
53
+
54
+ # Add conversation history
55
+ for user_msg, bot_msg in history:
56
+ if user_msg:
57
+ messages.append({"role": "user", "content": user_msg})
58
+ if bot_msg:
59
+ messages.append({"role": "assistant", "content": bot_msg})
60
+
61
+ # Add the current message
62
+ messages.append({"role": "user", "content": message})
63
+
64
+ try:
65
+ response = ""
66
+ for chunk in self.client.chat_completion(
67
+ messages,
68
+ max_tokens=max_tokens,
69
+ stream=True,
70
+ temperature=temperature,
71
+ top_p=top_p,
72
+ ):
73
+ token = chunk.choices[0].delta.content or ""
74
+ response += token
75
+ yield response
76
+ except Exception as e:
77
+ logger.error(f"Error generating response: {e}")
78
+ yield f"抱歉,发生了错误: {str(e)}"
79
 
80
+ def create_interface(self) -> gr.Blocks:
81
+ """Create and configure the chat interface."""
82
+ # Custom CSS for a modern look
83
+ custom_css = """
84
+ .chatbot .message {
85
+ border-radius: 12px;
86
+ margin: 8px;
87
+ padding: 12px;
88
+ box-shadow: 0 1px 3px rgba(0,0,0,0.12);
89
+ }
90
+ .chatbot .user-message {
91
+ background-color: #e3f2fd;
92
+ border-left: 4px solid #2196F3;
93
+ }
94
+ .chatbot .bot-message {
95
+ background-color: #f5f5f5;
96
+ border-left: 4px solid #9e9e9e;
97
+ }
98
+ .gr-button {
99
+ border-radius: 8px;
100
+ padding: 8px 16px;
101
+ transition: all 0.3s ease;
102
+ }
103
+ .gr-button:hover {
104
+ transform: translateY(-2px);
105
+ box-shadow: 0 4px 8px rgba(0,0,0,0.15);
106
+ }
107
+ .container {
108
+ max-width: 900px;
109
+ margin: 0 auto;
110
+ }
111
+ """
112
+
113
+ with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as interface:
114
+ gr.Markdown("# 喵哥Google-Gemma-3尝鲜版")
115
+ gr.Markdown("与Google Gemma 3 27B模型互动,可自定义参数。")
116
+
117
+ with gr.Row():
118
+ with gr.Column(scale=4):
119
+ chatbot = gr.Chatbot(
120
+ label="Gemma Chat",
121
+ avatar_images=("./user.png", "./botge.png"),
122
+ height=500,
123
+ show_copy_button=True,
124
+ elem_classes="chatbox"
125
+ )
126
+
127
+ with gr.Row():
128
+ with gr.Column(scale=8):
129
+ msg = gr.Textbox(
130
+ show_label=False,
131
+ placeholder="在这里输入您的消息...",
132
+ container=False,
133
+ lines=2
134
+ )
135
+ with gr.Column(scale=1, min_width=70):
136
+ submit_btn = gr.Button("发送", variant="primary")
137
+
138
+ with gr.Row():
139
+ clear_btn = gr.Button("清空对话", variant="secondary")
140
+ example_btn = gr.Button("加载示例", variant="secondary")
141
+
142
+ with gr.Column(scale=2):
143
+ with gr.Accordion("聊天设置", open=True):
144
+ system_msg = gr.Textbox(
145
+ value=ChatConfig.DEFAULT_SYSTEM_MSG,
146
+ label="系统提示词",
147
+ lines=3,
148
+ placeholder="输入系统提示词..."
149
+ )
150
+
151
+ with gr.Accordion("高级参数", open=False):
152
+ max_tokens = gr.Slider(
153
+ minimum=1,
154
+ maximum=8192,
155
+ value=ChatConfig.DEFAULT_MAX_TOKENS,
156
+ step=1,
157
+ label="最大标记数",
158
+ info="控制回复长度"
159
+ )
160
+ temperature = gr.Slider(
161
+ minimum=0.1,
162
+ maximum=1.0,
163
+ value=ChatConfig.DEFAULT_TEMP,
164
+ step=0.1,
165
+ label="温度",
166
+ info="控制随机性"
167
+ )
168
+ top_p = gr.Slider(
169
+ minimum=0.1,
170
+ maximum=1.0,
171
+ value=ChatConfig.DEFAULT_TOP_P,
172
+ step=0.05,
173
+ label="Top-P",
174
+ info="控制多样性"
175
+ )
176
+
177
+ with gr.Accordion("示例问题", open=False):
178
+ gr.Examples(
179
+ examples=[
180
+ ["讲一讲人工智能的最新进展"],
181
+ ["写一个关于机器���发现情感的短篇故事"],
182
+ ["向10岁的孩子解释量子计算"]
183
+ ],
184
+ inputs=msg
185
+ )
186
+
187
+ with gr.Accordion("模型信息", open=False):
188
+ gr.Markdown(f"""
189
+ - **模型**: {ChatConfig.MODEL}
190
+ - **提供商**: Hugging Face
191
+ - **描述**: Gemma 3是Google推出的先进开源大语言模型。
192
+ """)
193
+
194
+ # Set up event handlers
195
+ msg_and_submit = [msg, submit_btn]
196
+
197
+ submit_click = submit_btn.click(
198
+ fn=self.generate_response,
199
+ inputs=[msg, chatbot, system_msg, max_tokens, temperature, top_p],
200
+ outputs=chatbot,
201
+ api_name="chat"
202
  )
203
+
204
+ # Submit when pressing Enter (but not when pressing Shift+Enter)
205
+ msg.submit(
206
+ fn=self.generate_response,
207
+ inputs=[msg, chatbot, system_msg, max_tokens, temperature, top_p],
208
+ outputs=chatbot,
209
+ api_name=False
210
+ )
211
+
212
+ # Clear the textbox after sending
213
+ submit_click.then(lambda: "", None, msg)
214
+ msg.submit(lambda: "", None, msg)
215
+
216
+ # Clear chat button
217
+ clear_btn.click(lambda: None, None, chatbot)
218
+
219
+ # Example button
220
+ example_btn.click(
221
+ lambda: ("介绍一下人工智能研究中最有趣的发展", []),
222
+ None,
223
+ [msg, chatbot]
224
+ )
225
+
226
+ return interface
227
 
228
  def main():
229
+ """Main function to launch the application."""
230
+ try:
231
+ app = ChatApp()
232
+ interface = app.create_interface()
233
+
234
+ interface.launch(
235
+ server_name="0.0.0.0",
236
+ server_port=7860,
237
+ share=False,
238
+ show_api=False,
239
+ debug=True,
240
+ show_error=True
241
+ )
242
+ except Exception as e:
243
+ logger.critical(f"Failed to launch application: {e}")
244
+ print(f"错误: {e}")
245
 
246
  if __name__ == "__main__":
247
  main()