BryanBradfo commited on
Commit
7ff48bf
·
1 Parent(s): 9c86233

gemma chatbot

Browse files
Files changed (4) hide show
  1. app.py +138 -0
  2. gemma.png +0 -0
  3. requirements.txt +1 -0
  4. user.png +0 -0
app.py ADDED
@@ -0,0 +1,138 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from huggingface_hub import InferenceClient
2
+ from typing import List, Tuple
3
+ import gradio as gr
4
+
5
+ # Ensure you have the required libraries installed
6
+
7
+ # Chatbot configuration
8
+ class ChatConfig:
9
+ MODEL = "google/gemma-3-4b-it"
10
+ DEFAULT_SYSTEM_MSG = "You are a extremely smart and useful Chatbot."
11
+ DEFAULT_MAX_TOKENS = 1024
12
+ DEFAULT_TEMP = 0.4
13
+ DEFAULT_TOP_P = 0.95
14
+
15
+ client = InferenceClient(ChatConfig.MODEL)
16
+
17
+ def generate_response(
18
+ message: str,
19
+ history: List[Tuple[str, str]],
20
+ system_message: str = ChatConfig.DEFAULT_SYSTEM_MSG,
21
+ max_tokens: int = ChatConfig.DEFAULT_MAX_TOKENS,
22
+ temperature: float = ChatConfig.DEFAULT_TEMP,
23
+ top_p: float = ChatConfig.DEFAULT_TOP_P
24
+ ) -> str:
25
+
26
+ messages = [{"role": "system", "content": system_message}]
27
+
28
+ # Conversation history
29
+ for user_msg, bot_msg in history:
30
+ if user_msg:
31
+ messages.append({"role": "user", "content": user_msg})
32
+ if bot_msg:
33
+ messages.append({"role": "assistant", "content": bot_msg})
34
+
35
+ messages.append({"role": "user", "content": message})
36
+
37
+ response = ""
38
+
39
+ # Stream the response
40
+ # The stream=True parameter allows for real-time response generation
41
+ for chunk in client.chat_completion(
42
+ messages,
43
+ max_tokens=max_tokens,
44
+ stream=True,
45
+ temperature=temperature,
46
+ top_p=top_p,
47
+ ):
48
+ token = chunk.choices[0].delta.content or ""
49
+ response += token
50
+ yield response
51
+
52
+
53
+ def create_interface() -> gr.ChatInterface:
54
+ """Create and configure the chat interface."""
55
+ # Custom CSS for a modern look
56
+ custom_css = """
57
+ .chatbot .message {
58
+ border-radius: 12px;
59
+ margin: 5px;
60
+ padding: 10px;
61
+ }
62
+ .chatbot .user-message {
63
+ background-color: #e3f2fd;
64
+ }
65
+ .chatbot .bot-message {
66
+ background-color: #f5f5f5;
67
+ }
68
+ .gr-button {
69
+ border-radius: 8px;
70
+ padding: 8px 16px;
71
+ }
72
+ """
73
+
74
+ # Custom chatbot
75
+ chatbot = gr.Chatbot(
76
+ label="Gemma Chat",
77
+ avatar_images=("./user.png", "./gemma.png"),
78
+ height=450,
79
+ show_copy_button=True
80
+ )
81
+
82
+ # Chat interface
83
+ interface = gr.ChatInterface(
84
+ fn=generate_response,
85
+ chatbot=chatbot,
86
+ title="Your Gemma 3-(iend)",
87
+ theme=gr.themes.Soft(),
88
+ css=custom_css,
89
+ additional_inputs=[
90
+ gr.Textbox(
91
+ value=ChatConfig.DEFAULT_SYSTEM_MSG,
92
+ label="System Prompt (You can change the text below)",
93
+ lines=2,
94
+ placeholder="Enter system message..."
95
+ ),
96
+ gr.Slider(
97
+ minimum=1,
98
+ maximum=8192,
99
+ value=ChatConfig.DEFAULT_MAX_TOKENS,
100
+ step=1,
101
+ label="Max Tokens",
102
+ info="Controls response length"
103
+ ),
104
+ gr.Slider(
105
+ minimum=0.1,
106
+ maximum=1.0,
107
+ value=ChatConfig.DEFAULT_TEMP,
108
+ step=0.1,
109
+ label="Temperature",
110
+ info="Controls randomness"
111
+ ),
112
+ gr.Slider(
113
+ minimum=0.1,
114
+ maximum=1.0,
115
+ value=ChatConfig.DEFAULT_TOP_P,
116
+ step=0.05,
117
+ label="Top-P",
118
+ info="Controls diversity"
119
+ )
120
+ ],
121
+ additional_inputs_accordion=gr.Accordion(label="Advanced Settings", open=False)
122
+ )
123
+
124
+ return interface
125
+
126
+ def main():
127
+ app = create_interface()
128
+ app.launch(
129
+ server_name="0.0.0.0",
130
+ server_port=7860,
131
+ share=False,
132
+ show_api=False,
133
+ show_error=True,
134
+ debug=True
135
+ )
136
+
137
+ if __name__ == "__main__":
138
+ main()
gemma.png ADDED
requirements.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ huggingface_hub==0.29.3
user.png ADDED