asthaaa300
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,92 +1,243 @@
|
|
1 |
-
import
|
2 |
-
import
|
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 |
</div>
|
|
|
61 |
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
<div className="flex flex-col gap-2">
|
76 |
-
<button className="px-4 py-2 bg-[#1a365d] text-white rounded-lg hover:bg-[#2a4a7d] transition-colors">
|
77 |
-
Send
|
78 |
-
</button>
|
79 |
-
<button className="px-4 py-2 bg-[#b3c6d9] text-[#1a365d] rounded-lg hover:bg-[#99b3cc] transition-colors">
|
80 |
-
Clear
|
81 |
-
</button>
|
82 |
</div>
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
91 |
|
92 |
-
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import os
|
3 |
+
from huggingface_hub import InferenceClient
|
4 |
+
from huggingface_hub.inference._generated.types.chat_completion import ChatCompletionStreamOutput
|
5 |
+
|
6 |
+
MODEL = "nomiChroma3.1"
|
7 |
+
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
8 |
+
|
9 |
+
# Previous functions remain exactly the same
|
10 |
+
def respond(message: str, chat_history: list[dict]) -> tuple[list[dict], str]:
|
11 |
+
"""
|
12 |
+
Generate a response and update chat history.
|
13 |
+
Uses the new message format with role and content keys.
|
14 |
+
Returns tuple of (new_history, None) to clear input box.
|
15 |
+
"""
|
16 |
+
system_message = "You are a maritime legal assistant with expertise strictly in Indian maritime law. Provide a detailed legal advice and information based on Indian maritime legal principles and regulations."
|
17 |
+
|
18 |
+
messages = [{"role": "system", "content": system_message}]
|
19 |
+
for msg in chat_history:
|
20 |
+
messages.append({"role": msg["role"], "content": msg["content"]})
|
21 |
+
messages.append({"role": "user", "content": message})
|
22 |
+
|
23 |
+
chat_history = chat_history + [{"role": "user", "content": message}]
|
24 |
+
response = ""
|
25 |
+
|
26 |
+
try:
|
27 |
+
for chunk in client.chat_completion(
|
28 |
+
messages,
|
29 |
+
max_tokens=512,
|
30 |
+
stream=True,
|
31 |
+
temperature=0.7,
|
32 |
+
top_p=0.95,
|
33 |
+
):
|
34 |
+
try:
|
35 |
+
if isinstance(chunk, ChatCompletionStreamOutput):
|
36 |
+
content = chunk.choices[0].delta.content
|
37 |
+
if content:
|
38 |
+
response += content
|
39 |
+
if len(chat_history) > 0 and chat_history[-1]["role"] == "assistant":
|
40 |
+
chat_history[-1]["content"] = response
|
41 |
+
else:
|
42 |
+
chat_history.append({"role": "assistant", "content": response})
|
43 |
+
yield chat_history, ""
|
44 |
+
if chunk.choices[0].finish_reason == 'stop':
|
45 |
+
break
|
46 |
+
elif isinstance(chunk, dict):
|
47 |
+
content = chunk.get('choices', [{}])[0].get('delta', {}).get('content')
|
48 |
+
if content:
|
49 |
+
response += content
|
50 |
+
if len(chat_history) > 0 and chat_history[-1]["role"] == "assistant":
|
51 |
+
chat_history[-1]["content"] = response
|
52 |
+
else:
|
53 |
+
chat_history.append({"role": "assistant", "content": response})
|
54 |
+
yield chat_history, ""
|
55 |
+
if chunk.get('choices', [{}])[0].get('finish_reason') == 'stop':
|
56 |
+
break
|
57 |
+
elif isinstance(chunk, str) and chunk.strip():
|
58 |
+
response += chunk
|
59 |
+
if len(chat_history) > 0 and chat_history[-1]["role"] == "assistant":
|
60 |
+
chat_history[-1]["content"] = response
|
61 |
+
else:
|
62 |
+
chat_history.append({"role": "assistant", "content": response})
|
63 |
+
yield chat_history, ""
|
64 |
+
|
65 |
+
except Exception as e:
|
66 |
+
print(f"Error processing chunk: {e}")
|
67 |
+
continue
|
68 |
+
|
69 |
+
if not response:
|
70 |
+
chat_history.append({"role": "assistant", "content": "I apologize, but I couldn't generate a response. Please try again."})
|
71 |
+
|
72 |
+
yield chat_history, ""
|
73 |
+
|
74 |
+
except Exception as e:
|
75 |
+
error_msg = f"An error occurred: {str(e)}"
|
76 |
+
chat_history.append({"role": "assistant", "content": error_msg})
|
77 |
+
yield chat_history, ""
|
78 |
+
|
79 |
+
def handle_example_click(example_query: str):
|
80 |
+
"""Handle example query click by returning the query and empty chat history"""
|
81 |
+
return example_query, []
|
82 |
+
|
83 |
+
# Updated CSS with image styling
|
84 |
+
custom_css = """
|
85 |
+
@import url('https://fonts.googleapis.com/css2?family=Ubuntu:wght@300;400;500;700&display=swap');
|
86 |
+
|
87 |
+
.gradio-container {
|
88 |
+
background-color: #1a365d !important;
|
89 |
+
font-family: 'Ubuntu', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Cantarell, "Helvetica Neue", sans-serif !important;
|
90 |
+
}
|
91 |
+
|
92 |
+
.header-container {
|
93 |
+
text-align: center;
|
94 |
+
padding: 2rem 0;
|
95 |
+
margin-bottom: 1rem;
|
96 |
+
border-bottom: 2px solid rgba(255, 255, 255, 0.1);
|
97 |
+
position: relative;
|
98 |
+
z-index: 1;
|
99 |
+
}
|
100 |
+
|
101 |
+
.header-title {
|
102 |
+
color: #ffffff;
|
103 |
+
font-size: 2.5rem;
|
104 |
+
margin-bottom: 0.5rem;
|
105 |
+
font-family: 'Ubuntu', sans-serif !important;
|
106 |
+
text-shadow: 0 2px 4px rgba(0,0,0,0.2);
|
107 |
+
}
|
108 |
+
|
109 |
+
.header-subtitle {
|
110 |
+
color: #e6f3ff;
|
111 |
+
font-size: 1.1rem;
|
112 |
+
margin-bottom: 0.3rem;
|
113 |
+
font-family: 'Ubuntu', sans-serif !important;
|
114 |
+
opacity: 0.9;
|
115 |
+
}
|
116 |
+
|
117 |
+
.maritime-images {
|
118 |
+
display: grid;
|
119 |
+
grid-template-columns: repeat(3, 1fr);
|
120 |
+
gap: 1rem;
|
121 |
+
margin: 1.5rem auto;
|
122 |
+
max-width: 1200px;
|
123 |
+
padding: 0 1rem;
|
124 |
+
}
|
125 |
+
|
126 |
+
.maritime-image {
|
127 |
+
width: 100%;
|
128 |
+
height: 200px;
|
129 |
+
object-fit: cover;
|
130 |
+
border-radius: 8px;
|
131 |
+
border: 2px solid #e6f3ff;
|
132 |
+
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
|
133 |
+
transition: transform 0.3s ease;
|
134 |
+
filter: contrast(110%) saturate(110%);
|
135 |
+
}
|
136 |
+
|
137 |
+
.maritime-image:hover {
|
138 |
+
transform: scale(1.02);
|
139 |
+
}
|
140 |
+
|
141 |
+
/* Rest of your existing CSS remains exactly the same */
|
142 |
+
.sidebar {
|
143 |
+
background: #ccd9e6 !important;
|
144 |
+
border-radius: 8px !important;
|
145 |
+
padding: 15px !important;
|
146 |
+
border: 1px solid rgba(176, 226, 255, 0.2) !important;
|
147 |
+
height: fit-content !important;
|
148 |
+
position: relative;
|
149 |
+
overflow: hidden;
|
150 |
+
}
|
151 |
+
|
152 |
+
/* ... (rest of your existing CSS) ... */
|
153 |
+
"""
|
154 |
+
|
155 |
+
# Main application
|
156 |
+
with gr.Blocks(css=custom_css, theme=gr.themes.Base()) as demo:
|
157 |
+
# Updated header with realistic images
|
158 |
+
gr.HTML("""
|
159 |
+
<div class="header-container">
|
160 |
+
<h1 class="header-title">Maritime Legal Compliance</h1>
|
161 |
+
<div class="maritime-images">
|
162 |
+
<img src="/api/placeholder/400/320" alt="Modern port with cargo vessels" class="maritime-image">
|
163 |
+
<img src="/api/placeholder/400/320" alt="Container ship at sea" class="maritime-image">
|
164 |
+
<img src="/api/placeholder/400/320" alt="Coastal waterway" class="maritime-image">
|
165 |
+
</div>
|
166 |
+
<p class="header-subtitle">AI-powered assistance for Indian maritime law queries</p>
|
167 |
+
<p class="header-subtitle">This chatbot uses Fine-tuned LLAMA-3.1 model personalised specifically to provide assistance with Indian maritime legal queries.</p>
|
168 |
</div>
|
169 |
+
""")
|
170 |
|
171 |
+
# Rest of your application remains exactly the same
|
172 |
+
with gr.Row():
|
173 |
+
with gr.Column(scale=1, elem_classes="sidebar"):
|
174 |
+
gr.HTML("""
|
175 |
+
<div class="sidebar-bg">
|
176 |
+
<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%">
|
177 |
+
<pattern id="grid" width="10" height="10" patternUnits="userSpaceOnUse">
|
178 |
+
<path d="M 10 0 L 0 0 0 10" fill="none" stroke="#1a365d" stroke-width="0.5"/>
|
179 |
+
</pattern>
|
180 |
+
<rect width="100%" height="100%" fill="url(#grid)"/>
|
181 |
+
<circle cx="80" cy="20" r="15" fill="#1a365d"/>
|
182 |
+
<path d="M70,80 L90,80 L80,95 Z" fill="#1a365d"/>
|
183 |
+
</svg>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
184 |
</div>
|
185 |
+
<div class="sidebar-content">
|
186 |
+
""")
|
187 |
+
|
188 |
+
gr.HTML("""
|
189 |
+
<div class="sidebar-title">
|
190 |
+
<svg class="sidebar-icon" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
|
191 |
+
<circle cx="12" cy="12" r="10" fill="none" stroke="#1a365d" stroke-width="2"/>
|
192 |
+
<path d="M12 6 L12 18 M6 12 L18 12" stroke="#1a365d" stroke-width="2"/>
|
193 |
+
</svg>
|
194 |
+
Example Queries
|
195 |
+
</div>
|
196 |
+
""")
|
197 |
+
|
198 |
+
example_queries = [
|
199 |
+
"What are the key regulations governing ports in India?",
|
200 |
+
"What are the legal requirements for registering a vessel in India?",
|
201 |
+
"What are the recent environmental concerns for shipping in Indian waters?",
|
202 |
+
"What are the rules for coastal cargo transportation in India?"
|
203 |
+
]
|
204 |
+
|
205 |
+
with gr.Column(elem_classes="example-queries"):
|
206 |
+
example_buttons = [gr.Button(query, elem_classes="example-query-button") for query in example_queries]
|
207 |
+
|
208 |
+
gr.HTML("</div>")
|
209 |
+
|
210 |
+
with gr.Column(scale=3):
|
211 |
+
chatbot = gr.Chatbot(
|
212 |
+
height=300,
|
213 |
+
elem_classes="chat-container",
|
214 |
+
type="messages"
|
215 |
+
)
|
216 |
+
msg = gr.Textbox(
|
217 |
+
show_label=False,
|
218 |
+
placeholder="Type your maritime law query here...",
|
219 |
+
container=False
|
220 |
+
)
|
221 |
+
with gr.Row():
|
222 |
+
submit = gr.Button("Send", variant="primary")
|
223 |
+
clear = gr.Button("Clear")
|
224 |
+
|
225 |
+
# Event handlers remain the same
|
226 |
+
msg.submit(fn=respond, inputs=[msg, chatbot], outputs=[chatbot, msg])
|
227 |
+
submit.click(fn=respond, inputs=[msg, chatbot], outputs=[chatbot, msg])
|
228 |
+
clear.click(fn=lambda: ([], ""), inputs=None, outputs=[chatbot, msg], queue=False)
|
229 |
+
|
230 |
+
for button in example_buttons:
|
231 |
+
button.click(
|
232 |
+
fn=handle_example_click,
|
233 |
+
inputs=[button],
|
234 |
+
outputs=[msg, chatbot],
|
235 |
+
queue=False
|
236 |
+
).then(
|
237 |
+
fn=respond,
|
238 |
+
inputs=[msg, chatbot],
|
239 |
+
outputs=[chatbot, msg]
|
240 |
+
)
|
241 |
|
242 |
+
if __name__ == "__main__":
|
243 |
+
demo.launch()
|