alinice8 commited on
Commit
af710be
·
verified ·
1 Parent(s): 104102c
Files changed (1) hide show
  1. app.py +277 -0
app.py ADDED
@@ -0,0 +1,277 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import gradio as gr
3
+ from threading import Thread
4
+ from transformers import BitsAndBytesConfig
5
+ from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline, TextStreamer, TextIteratorStreamer
6
+
7
+ model_id = "large-traversaal/Alif-1.0-8B-Instruct"
8
+
9
+ # 4-bit quantization configuration
10
+ quantization_config = BitsAndBytesConfig(
11
+ load_in_4bit=True,
12
+ bnb_4bit_compute_dtype=torch.float16,
13
+ bnb_4bit_use_double_quant=True,
14
+ bnb_4bit_quant_type="nf4"
15
+ )
16
+
17
+ # Load tokenizer and model in 4-bit
18
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
19
+ model = AutoModelForCausalLM.from_pretrained(
20
+ model_id,
21
+ quantization_config=quantization_config,
22
+ device_map="auto"
23
+ )
24
+
25
+ chat_prompt = """
26
+ You are JusticeGPT, an AI assistant with the wisdom and authority of a judge. You provide balanced, fair, and thoughtful responses.
27
+ ### Instruction:
28
+ Below is an instruction that describes a task. Write a response in urdu that appropriately completes the request. Don't say you don't know unless you really don't.
29
+ Please be expressive when needed. Give long and detailed answers.
30
+ ### Input:
31
+ {prompt}
32
+ ### Response:
33
+ """
34
+
35
+ def generate_response(message, history):
36
+ prompt = chat_prompt.format(prompt=message)
37
+ inputs = tokenizer([prompt], return_tensors="pt").to("cuda" if torch.cuda.is_available() else "cpu")
38
+
39
+ streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
40
+
41
+ generation_kwargs = dict(
42
+ inputs,
43
+ streamer=streamer,
44
+ max_new_tokens=4098,
45
+ do_sample=True,
46
+ top_p=0.95,
47
+ top_k=50,
48
+ temperature=0.7,
49
+ repetition_penalty=1.2,
50
+ )
51
+
52
+ thread = Thread(target=model.generate, kwargs=generation_kwargs)
53
+ thread.start()
54
+
55
+ generated_text = ""
56
+ for new_text in streamer:
57
+ if new_text.endswith(tokenizer.eos_token):
58
+ new_text = new_text[:len(new_text) - len(tokenizer.eos_token)]
59
+ generated_text += new_text
60
+ yield generated_text
61
+
62
+ # Custom CSS for the judge theme
63
+ custom_css = """
64
+ # Judge-themed CSS
65
+ .gradio-container {
66
+ background: linear-gradient(135deg, #2c3e50 0%, #3498db 100%);
67
+ font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
68
+ }
69
+
70
+ #header {
71
+ text-align: center;
72
+ background: linear-gradient(135deg, #8B4513 0%, #D2691E 100%);
73
+ padding: 20px;
74
+ border-radius: 10px;
75
+ margin-bottom: 20px;
76
+ color: white;
77
+ box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
78
+ }
79
+
80
+ #judge-img {
81
+ max-width: 150px;
82
+ border-radius: 50%;
83
+ border: 5px solid gold;
84
+ margin: 0 auto;
85
+ display: block;
86
+ }
87
+
88
+ .chatbot {
89
+ background: white;
90
+ border-radius: 15px;
91
+ box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
92
+ min-height: 500px;
93
+ }
94
+
95
+ .chatbot .message {
96
+ padding: 15px;
97
+ margin: 10px;
98
+ border-radius: 10px;
99
+ }
100
+
101
+ .chatbot .user {
102
+ background: #e3f2fd;
103
+ border-left: 4px solid #2196F3;
104
+ }
105
+
106
+ .chatbot .bot {
107
+ background: #f3e5f5;
108
+ border-left: 4px solid #9C27B0;
109
+ }
110
+
111
+ #input-box {
112
+ background: white;
113
+ border-radius: 10px;
114
+ padding: 15px;
115
+ box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
116
+ }
117
+
118
+ #examples {
119
+ background: rgba(255, 255, 255, 0.9);
120
+ border-radius: 10px;
121
+ padding: 15px;
122
+ margin-top: 20px;
123
+ }
124
+
125
+ .examples-title {
126
+ font-weight: bold;
127
+ color: #2c3e50;
128
+ margin-bottom: 10px;
129
+ }
130
+
131
+ .examples-container {
132
+ display: flex;
133
+ flex-direction: column;
134
+ gap: 8px;
135
+ }
136
+
137
+ .example-btn {
138
+ background: #3498db;
139
+ color: white;
140
+ border: none;
141
+ padding: 8px 15px;
142
+ border-radius: 20px;
143
+ cursor: pointer;
144
+ transition: all 0.3s ease;
145
+ }
146
+
147
+ .example-btn:hover {
148
+ background: #2980b9;
149
+ transform: translateY(-2px);
150
+ }
151
+
152
+ #submit-btn {
153
+ background: linear-gradient(135deg, #27ae60 0%, #2ecc71 100%);
154
+ color: white;
155
+ border: none;
156
+ padding: 12px 30px;
157
+ border-radius: 25px;
158
+ font-weight: bold;
159
+ transition: all 0.3s ease;
160
+ }
161
+
162
+ #submit-btn:hover {
163
+ transform: translateY(-2px);
164
+ box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
165
+ }
166
+
167
+ #clear-btn {
168
+ background: linear-gradient(135deg, #e74c3c 0%, #c0392b 100%);
169
+ color: white;
170
+ border: none;
171
+ padding: 12px 30px;
172
+ border-radius: 25px;
173
+ font-weight: bold;
174
+ transition: all 0.3s ease;
175
+ }
176
+
177
+ #clear-btn:hover {
178
+ transform: translateY(-2px);
179
+ box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
180
+ }
181
+ """
182
+
183
+ # Judge image URL (you can replace this with your own image)
184
+ judge_image = "https://images.unsplash.com/photo-1589829545856-d10d557cf95f?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=500&q=80"
185
+
186
+ # Create the enhanced interface
187
+ with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
188
+ # Header section with judge image and title
189
+ with gr.Column(elem_id="header"):
190
+ gr.HTML(f"""
191
+ <div style="text-align: center;">
192
+ <img src="{judge_image}" id="judge-img" alt="Justice GPT">
193
+ <h1 style="margin: 10px 0; font-size: 2.5em; text-shadow: 2px 2px 4px rgba(0,0,0,0.3);">⚖️ JusticeGPT ⚖️</h1>
194
+ <p style="font-size: 1.2em; opacity: 0.9;">Your AI Assistant with Judicial Wisdom</p>
195
+ </div>
196
+ """)
197
+
198
+ # Main chat area
199
+ chatbot = gr.Chatbot(
200
+ label="JusticeGPT Conversation",
201
+ height=500,
202
+ show_copy_button=True,
203
+ show_share_button=True
204
+ )
205
+
206
+ # Input area
207
+ with gr.Row():
208
+ msg = gr.Textbox(
209
+ label="Your Question",
210
+ placeholder="Ask JusticeGPT anything in Urdu or English...",
211
+ lines=2,
212
+ max_lines=5,
213
+ scale=4,
214
+ container=False,
215
+ elem_id="input-box"
216
+ )
217
+
218
+ # Buttons
219
+ with gr.Row():
220
+ submit_btn = gr.Button("⚡ Submit Question", elem_id="submit-btn", scale=1)
221
+ clear_btn = gr.Button("🗑️ Clear Chat", elem_id="clear-btn", scale=1)
222
+
223
+ # Examples section
224
+ with gr.Column(elem_id="examples"):
225
+ gr.Markdown("### 💡 Example Questions")
226
+ with gr.Row():
227
+ gr.Examples(
228
+ examples=[
229
+ "شہر کراچی کی کیا اہمیت ہے؟",
230
+ "امریکی آئین کی بنیادی خصوصیات کیا ہیں؟",
231
+ "کیا امتحان میں نقل کرنا جائز ہے؟",
232
+ "What are the principles of justice?",
233
+ "How does the judicial system work?",
234
+ "Explain the concept of fairness in law"
235
+ ],
236
+ inputs=msg,
237
+ label="Click any example to try:",
238
+ examples_per_page=6
239
+ )
240
+
241
+ # Event handlers
242
+ def respond(message, chat_history):
243
+ bot_message = ""
244
+ for response in generate_response(message, chat_history):
245
+ bot_message = response
246
+ chat_history.append((message, bot_message))
247
+ return "", chat_history
248
+
249
+ def clear_chat():
250
+ return []
251
+
252
+ # Connect the interface
253
+ submit_btn.click(
254
+ respond,
255
+ [msg, chatbot],
256
+ [msg, chatbot]
257
+ )
258
+
259
+ msg.submit(
260
+ respond,
261
+ [msg, chatbot],
262
+ [msg, chatbot]
263
+ )
264
+
265
+ clear_btn.click(
266
+ clear_chat,
267
+ outputs=chatbot
268
+ )
269
+
270
+ # Launch the interface
271
+ if __name__ == "__main__":
272
+ demo.launch(
273
+ share=True,
274
+ server_name="0.0.0.0",
275
+ server_port=7860,
276
+ show_error=True
277
+ )