Update app.py
Browse files
app.py
CHANGED
@@ -6,29 +6,34 @@ from huggingface_hub.inference._generated.types.chat_completion import ChatCompl
|
|
6 |
MODEL = "nomiChroma3.1"
|
7 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
8 |
|
9 |
-
def respond(
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
messages.append({"role": "user", "content": message})
|
18 |
-
|
19 |
response = ""
|
20 |
try:
|
21 |
for message in client.chat_completion(
|
22 |
messages,
|
23 |
-
max_tokens=
|
24 |
stream=True,
|
25 |
-
temperature=
|
26 |
-
top_p=
|
27 |
):
|
28 |
try:
|
29 |
if isinstance(message, ChatCompletionStreamOutput):
|
30 |
content = message.choices[0].delta.content
|
31 |
-
if content:
|
32 |
response += content
|
33 |
yield response
|
34 |
if message.choices[0].finish_reason == 'stop':
|
@@ -40,120 +45,165 @@ def respond(message, history, config):
|
|
40 |
yield response
|
41 |
if message.get('choices', [{}])[0].get('finish_reason') == 'stop':
|
42 |
break
|
43 |
-
elif isinstance(message, str)
|
44 |
-
|
45 |
-
|
|
|
46 |
except Exception as e:
|
47 |
print(f"Error processing message: {e}")
|
48 |
continue
|
49 |
-
|
50 |
if response:
|
51 |
yield response
|
|
|
52 |
except Exception as e:
|
53 |
print(f"An error occurred: {e}")
|
54 |
yield f"An error occurred: {e}"
|
55 |
|
56 |
-
# Streamlined CSS with better performance
|
57 |
custom_css = """
|
58 |
-
|
59 |
-
|
|
|
|
|
|
|
|
|
60 |
.header-container {
|
61 |
text-align: center;
|
62 |
-
padding:
|
63 |
-
margin-bottom:
|
64 |
-
border-bottom:
|
65 |
}
|
66 |
-
|
67 |
.header-title {
|
68 |
-
color: #
|
69 |
-
font-size: 2.
|
70 |
margin-bottom: 0.5rem;
|
71 |
}
|
72 |
-
|
73 |
.header-subtitle {
|
74 |
color: #e6f3ff;
|
75 |
-
font-size: 1rem;
|
76 |
}
|
77 |
-
|
78 |
-
/*
|
79 |
.sidebar {
|
80 |
-
background:
|
81 |
-
border-radius:
|
82 |
-
padding:
|
83 |
border: 1px solid rgba(176, 226, 255, 0.2) !important;
|
84 |
-
|
85 |
-
overflow-y: auto !important;
|
86 |
-
position: sticky !important;
|
87 |
-
top: 1rem !important;
|
88 |
}
|
89 |
-
|
90 |
-
.sidebar-section {
|
91 |
-
margin-bottom: 1.5rem !important;
|
92 |
-
}
|
93 |
-
|
94 |
.sidebar-title {
|
95 |
-
|
96 |
-
|
97 |
-
margin-bottom:
|
98 |
padding-bottom: 0.5rem !important;
|
99 |
-
border-bottom:
|
100 |
}
|
101 |
-
|
102 |
-
/*
|
103 |
-
.sidebar
|
104 |
-
|
|
|
|
|
|
|
|
|
|
|
105 |
}
|
106 |
-
|
107 |
.sidebar label {
|
108 |
-
|
109 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
110 |
}
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
/* Chat elements */
|
115 |
.chat-container {
|
116 |
background: rgba(176, 226, 255, 0.15) !important;
|
117 |
-
border-radius:
|
118 |
-
padding:
|
119 |
-
height:
|
|
|
120 |
border: 1px solid rgba(176, 226, 255, 0.2) !important;
|
|
|
121 |
}
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
|
|
|
|
127 |
}
|
128 |
-
|
129 |
-
.message.user {
|
130 |
-
|
131 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
132 |
textarea {
|
133 |
background-color: #e6f3ff !important;
|
134 |
border: 1px solid rgba(176, 226, 255, 0.3) !important;
|
135 |
-
border-radius:
|
136 |
-
padding:
|
|
|
137 |
}
|
138 |
-
|
|
|
139 |
.gr-button {
|
140 |
background-color: #cce7ff !important;
|
141 |
color: #1a365d !important;
|
142 |
-
|
143 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
144 |
}
|
145 |
"""
|
146 |
|
147 |
-
# Main application
|
148 |
with gr.Blocks(css=custom_css, theme=gr.themes.Base()) as demo:
|
149 |
-
# Application state
|
150 |
-
config = gr.State({
|
151 |
-
"system_message": "You are a maritime legal assistant with expertise strictly in Indian maritime law. Provide detailed legal advice and information based on Indian maritime legal principles and regulations.",
|
152 |
-
"max_tokens": 512,
|
153 |
-
"temperature": 0.7,
|
154 |
-
"top_p": 0.95
|
155 |
-
})
|
156 |
-
|
157 |
# Header
|
158 |
gr.HTML("""
|
159 |
<div class="header-container">
|
@@ -162,95 +212,73 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Base()) as demo:
|
|
162 |
</div>
|
163 |
""")
|
164 |
|
|
|
165 |
with gr.Row():
|
166 |
-
#
|
167 |
with gr.Column(scale=1, elem_classes="sidebar"):
|
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 |
-
top_p = gr.Slider(
|
196 |
-
minimum=0.1,
|
197 |
-
maximum=1.0,
|
198 |
-
value=config.value["top_p"],
|
199 |
-
step=0.05,
|
200 |
-
label="Focus"
|
201 |
-
)
|
202 |
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
"What are the key regulations governing ports in India?",
|
207 |
-
"Explain the concept of cabotage in Indian maritime law.",
|
208 |
-
"What are the legal requirements for registering a vessel in India?"
|
209 |
-
]
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
elem_classes="example-button"
|
214 |
-
)
|
215 |
|
216 |
# Main chat area
|
217 |
with gr.Column(scale=3):
|
218 |
-
chatbot = gr.Chatbot(elem_classes="chat-container")
|
|
|
|
|
|
|
|
|
|
|
219 |
with gr.Row():
|
220 |
-
msg = gr.Textbox(
|
221 |
-
show_label=False,
|
222 |
-
placeholder="Type your maritime law query here...",
|
223 |
-
container=False
|
224 |
-
)
|
225 |
submit = gr.Button("Send", variant="primary")
|
226 |
-
|
227 |
-
clear = gr.Button("Clear History")
|
228 |
-
|
229 |
-
# Event handlers
|
230 |
-
def update_config(system_msg, max_tok, temp, tp):
|
231 |
-
return {"system_message": system_msg, "max_tokens": max_tok, "temperature": temp, "top_p": tp}
|
232 |
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
clear.click(lambda: None, None, chatbot, queue=False)
|
246 |
-
|
247 |
-
# Update config when settings change
|
248 |
-
for setting in [system_message, max_tokens, temperature, top_p]:
|
249 |
-
setting.change(
|
250 |
-
update_config,
|
251 |
-
inputs=[system_message, max_tokens, temperature, top_p],
|
252 |
-
outputs=[config]
|
253 |
-
)
|
254 |
|
255 |
if __name__ == "__main__":
|
256 |
demo.launch()
|
|
|
6 |
MODEL = "nomiChroma3.1"
|
7 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
8 |
|
9 |
+
def respond(
|
10 |
+
message,
|
11 |
+
history: list[tuple[str, str]],
|
12 |
+
system_message,
|
13 |
+
max_tokens,
|
14 |
+
temperature,
|
15 |
+
top_p,
|
16 |
+
):
|
17 |
+
messages = [{"role": "system", "content": system_message}]
|
18 |
+
for val in history:
|
19 |
+
if val[0]:
|
20 |
+
messages.append({"role": "user", "content": val[0]})
|
21 |
+
if val[1]:
|
22 |
+
messages.append({"role": "assistant", "content": val[1]})
|
23 |
messages.append({"role": "user", "content": message})
|
|
|
24 |
response = ""
|
25 |
try:
|
26 |
for message in client.chat_completion(
|
27 |
messages,
|
28 |
+
max_tokens=max_tokens,
|
29 |
stream=True,
|
30 |
+
temperature=temperature,
|
31 |
+
top_p=top_p,
|
32 |
):
|
33 |
try:
|
34 |
if isinstance(message, ChatCompletionStreamOutput):
|
35 |
content = message.choices[0].delta.content
|
36 |
+
if content is not None:
|
37 |
response += content
|
38 |
yield response
|
39 |
if message.choices[0].finish_reason == 'stop':
|
|
|
45 |
yield response
|
46 |
if message.get('choices', [{}])[0].get('finish_reason') == 'stop':
|
47 |
break
|
48 |
+
elif isinstance(message, str):
|
49 |
+
if message.strip():
|
50 |
+
response += message
|
51 |
+
yield response
|
52 |
except Exception as e:
|
53 |
print(f"Error processing message: {e}")
|
54 |
continue
|
55 |
+
|
56 |
if response:
|
57 |
yield response
|
58 |
+
|
59 |
except Exception as e:
|
60 |
print(f"An error occurred: {e}")
|
61 |
yield f"An error occurred: {e}"
|
62 |
|
|
|
63 |
custom_css = """
|
64 |
+
/* Global styles */
|
65 |
+
.gradio-container {
|
66 |
+
background-color: #1a365d !important;
|
67 |
+
}
|
68 |
+
|
69 |
+
/* Header styling */
|
70 |
.header-container {
|
71 |
text-align: center;
|
72 |
+
padding: 2rem 0;
|
73 |
+
margin-bottom: 2rem;
|
74 |
+
border-bottom: 2px solid rgba(255, 255, 255, 0.1);
|
75 |
}
|
76 |
+
|
77 |
.header-title {
|
78 |
+
color: #ffffff;
|
79 |
+
font-size: 2.5rem;
|
80 |
margin-bottom: 0.5rem;
|
81 |
}
|
82 |
+
|
83 |
.header-subtitle {
|
84 |
color: #e6f3ff;
|
85 |
+
font-size: 1.1rem;
|
86 |
}
|
87 |
+
|
88 |
+
/* Sidebar styling */
|
89 |
.sidebar {
|
90 |
+
background: #e6f3ff !important;
|
91 |
+
border-radius: 12px !important;
|
92 |
+
padding: 20px !important;
|
93 |
border: 1px solid rgba(176, 226, 255, 0.2) !important;
|
94 |
+
height: fit-content !important;
|
|
|
|
|
|
|
95 |
}
|
96 |
+
|
|
|
|
|
|
|
|
|
97 |
.sidebar-title {
|
98 |
+
color: #1a365d !important;
|
99 |
+
font-size: 1.2rem !important;
|
100 |
+
margin-bottom: 1rem !important;
|
101 |
padding-bottom: 0.5rem !important;
|
102 |
+
border-bottom: 2px solid rgba(26, 54, 93, 0.1) !important;
|
103 |
}
|
104 |
+
|
105 |
+
/* Input and control styling in sidebar */
|
106 |
+
.sidebar textarea,
|
107 |
+
.sidebar input,
|
108 |
+
.sidebar .gr-slider {
|
109 |
+
background-color: #ffffff !important;
|
110 |
+
border: 1px solid rgba(26, 54, 93, 0.2) !important;
|
111 |
+
border-radius: 8px !important;
|
112 |
+
color: #1a365d !important;
|
113 |
}
|
114 |
+
|
115 |
.sidebar label {
|
116 |
+
color: #1a365d !important;
|
117 |
+
font-weight: 500 !important;
|
118 |
+
}
|
119 |
+
|
120 |
+
/* Example queries styling */
|
121 |
+
.sidebar .gr-examples {
|
122 |
+
background-color: #ffffff !important;
|
123 |
+
border-radius: 8px !important;
|
124 |
+
padding: 12px !important;
|
125 |
+
margin-top: 1rem !important;
|
126 |
+
}
|
127 |
+
|
128 |
+
.sidebar .gr-examples button {
|
129 |
+
background-color: #cce7ff !important;
|
130 |
+
color: #1a365d !important;
|
131 |
+
border: none !important;
|
132 |
+
margin: 4px 0 !important;
|
133 |
+
padding: 8px 12px !important;
|
134 |
+
border-radius: 6px !important;
|
135 |
+
text-align: left !important;
|
136 |
+
width: 100% !important;
|
137 |
+
}
|
138 |
+
|
139 |
+
.sidebar .gr-examples button:hover {
|
140 |
+
background-color: #b0e2ff !important;
|
141 |
}
|
142 |
+
|
143 |
+
/* Chat container */
|
|
|
|
|
144 |
.chat-container {
|
145 |
background: rgba(176, 226, 255, 0.15) !important;
|
146 |
+
border-radius: 12px !important;
|
147 |
+
padding: 20px !important;
|
148 |
+
height: 600px !important;
|
149 |
+
overflow-y: auto !important;
|
150 |
border: 1px solid rgba(176, 226, 255, 0.2) !important;
|
151 |
+
backdrop-filter: blur(10px) !important;
|
152 |
}
|
153 |
+
|
154 |
+
/* Message styling */
|
155 |
+
.message.user, .message.bot {
|
156 |
+
padding: 12px 16px !important;
|
157 |
+
margin: 8px 0 !important;
|
158 |
+
border-radius: 8px !important;
|
159 |
+
color: #1a365d !important;
|
160 |
}
|
161 |
+
|
162 |
+
.message.user {
|
163 |
+
background-color: #cce7ff !important;
|
164 |
+
}
|
165 |
+
|
166 |
+
.message.bot {
|
167 |
+
background-color: #e6f3ff !important;
|
168 |
+
}
|
169 |
+
|
170 |
+
/* Chat input styling */
|
171 |
textarea {
|
172 |
background-color: #e6f3ff !important;
|
173 |
border: 1px solid rgba(176, 226, 255, 0.3) !important;
|
174 |
+
border-radius: 8px !important;
|
175 |
+
padding: 12px !important;
|
176 |
+
color: #1a365d !important;
|
177 |
}
|
178 |
+
|
179 |
+
/* Button styling */
|
180 |
.gr-button {
|
181 |
background-color: #cce7ff !important;
|
182 |
color: #1a365d !important;
|
183 |
+
border: none !important;
|
184 |
+
}
|
185 |
+
|
186 |
+
.gr-button:hover {
|
187 |
+
background-color: #b0e2ff !important;
|
188 |
+
}
|
189 |
+
|
190 |
+
/* Scrollbar styling */
|
191 |
+
::-webkit-scrollbar {
|
192 |
+
width: 8px;
|
193 |
+
}
|
194 |
+
|
195 |
+
::-webkit-scrollbar-track {
|
196 |
+
background: rgba(176, 226, 255, 0.1);
|
197 |
+
}
|
198 |
+
|
199 |
+
::-webkit-scrollbar-thumb {
|
200 |
+
background: rgba(176, 226, 255, 0.3);
|
201 |
+
border-radius: 4px;
|
202 |
}
|
203 |
"""
|
204 |
|
205 |
+
# Main application
|
206 |
with gr.Blocks(css=custom_css, theme=gr.themes.Base()) as demo:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
207 |
# Header
|
208 |
gr.HTML("""
|
209 |
<div class="header-container">
|
|
|
212 |
</div>
|
213 |
""")
|
214 |
|
215 |
+
# Main layout with sidebar
|
216 |
with gr.Row():
|
217 |
+
# Sidebar
|
218 |
with gr.Column(scale=1, elem_classes="sidebar"):
|
219 |
+
gr.Markdown("### Configuration", elem_classes="sidebar-title")
|
220 |
+
system_message = gr.Textbox(
|
221 |
+
value="You are a maritime legal assistant with expertise strictly in Indian maritime law. Provide detailed legal advice and information based on Indian maritime legal principles and regulations.",
|
222 |
+
label="System Prompt",
|
223 |
+
lines=4
|
224 |
+
)
|
225 |
+
max_tokens = gr.Slider(
|
226 |
+
minimum=1,
|
227 |
+
maximum=2048,
|
228 |
+
value=512,
|
229 |
+
step=1,
|
230 |
+
label="Response Length"
|
231 |
+
)
|
232 |
+
temperature = gr.Slider(
|
233 |
+
minimum=0.1,
|
234 |
+
maximum=4.0,
|
235 |
+
value=0.7,
|
236 |
+
step=0.1,
|
237 |
+
label="Creativity Level"
|
238 |
+
)
|
239 |
+
top_p = gr.Slider(
|
240 |
+
minimum=0.1,
|
241 |
+
maximum=1.0,
|
242 |
+
value=0.95,
|
243 |
+
step=0.05,
|
244 |
+
label="Response Focus"
|
245 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
246 |
|
247 |
+
gr.Markdown("### Example Queries", elem_classes="sidebar-title")
|
248 |
+
examples = gr.Examples(
|
249 |
+
examples=[
|
250 |
+
["What are the key regulations governing ports in India?"],
|
251 |
+
["Explain the concept of cabotage in Indian maritime law."],
|
252 |
+
["What are the legal requirements for registering a vessel in India?"],
|
253 |
+
],
|
254 |
+
inputs=[system_message],
|
255 |
+
label="Click on any example to load it"
|
256 |
+
)
|
|
|
|
|
257 |
|
258 |
# Main chat area
|
259 |
with gr.Column(scale=3):
|
260 |
+
chatbot = gr.Chatbot(height=500, elem_classes="chat-container")
|
261 |
+
msg = gr.Textbox(
|
262 |
+
show_label=False,
|
263 |
+
placeholder="Type your maritime law query here...",
|
264 |
+
container=False
|
265 |
+
)
|
266 |
with gr.Row():
|
|
|
|
|
|
|
|
|
|
|
267 |
submit = gr.Button("Send", variant="primary")
|
268 |
+
clear = gr.Button("Clear")
|
|
|
|
|
|
|
|
|
|
|
269 |
|
270 |
+
# Event handlers
|
271 |
+
submit_click = submit.click(
|
272 |
+
respond,
|
273 |
+
inputs=[msg, chatbot, system_message, max_tokens, temperature, top_p],
|
274 |
+
outputs=chatbot
|
275 |
+
)
|
276 |
+
msg_submit = msg.submit(
|
277 |
+
respond,
|
278 |
+
inputs=[msg, chatbot, system_message, max_tokens, temperature, top_p],
|
279 |
+
outputs=chatbot
|
280 |
+
)
|
281 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
282 |
|
283 |
if __name__ == "__main__":
|
284 |
demo.launch()
|