Tomoniai commited on
Commit
b14a2f9
·
verified ·
1 Parent(s): e16e24c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +133 -0
app.py ADDED
@@ -0,0 +1,133 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ type="messages",
72
+ avatar_images=("./user.png", "./botge.png"),
73
+ height=450,
74
+ show_copy_button=True
75
+ )
76
+
77
+ # Chat interface
78
+ interface = gr.ChatInterface(
79
+ fn=generate_response,
80
+ chatbot=chatbot,
81
+ title="Tomoniai's chat with Google-Gemma-3",
82
+ theme=gr.themes.Soft(),
83
+ css=custom_css,
84
+ additional_inputs=[
85
+ gr.Textbox(
86
+ value=ChatConfig.DEFAULT_SYSTEM_MSG,
87
+ label="System Prompt",
88
+ lines=2,
89
+ placeholder="Enter system message..."
90
+ ),
91
+ gr.Slider(
92
+ minimum=1,
93
+ maximum=8196,
94
+ value=ChatConfig.DEFAULT_MAX_TOKENS,
95
+ step=1,
96
+ label="Max Tokens",
97
+ info="Controls response length"
98
+ ),
99
+ gr.Slider(
100
+ minimum=0.1,
101
+ maximum=1.0,
102
+ value=ChatConfig.DEFAULT_TEMP,
103
+ step=0.1,
104
+ label="Temperature",
105
+ info="Controls randomness"
106
+ ),
107
+ gr.Slider(
108
+ minimum=0.1,
109
+ maximum=1.0,
110
+ value=ChatConfig.DEFAULT_TOP_P,
111
+ step=0.05,
112
+ label="Top-P",
113
+ info="Controls diversity"
114
+ )
115
+ ],
116
+ additional_inputs_accordion=gr.Accordion(label="Advanced Settings", open=False)
117
+ )
118
+
119
+ return interface
120
+
121
+ def main():
122
+ app = create_interface()
123
+ app.launch(
124
+ server_name="0.0.0.0",
125
+ server_port=7860,
126
+ share=False,
127
+ show_api=False,
128
+ show_error=True,
129
+ debug=True
130
+ )
131
+
132
+ if __name__ == "__main__":
133
+ main()