Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -14,9 +14,9 @@ class RealTimeFactChecker:
|
|
14 |
"""Initialize Groq client with API key"""
|
15 |
try:
|
16 |
self.client = Groq(api_key=api_key)
|
17 |
-
return True,
|
18 |
except Exception as e:
|
19 |
-
return False, f
|
20 |
|
21 |
def get_system_prompt(self):
|
22 |
"""Get the system prompt for consistent behavior"""
|
@@ -38,7 +38,7 @@ WHEN TO SEARCH:
|
|
38 |
- Weather conditions or forecasts
|
39 |
- Recent scientific discoveries or research
|
40 |
- Current political developments
|
41 |
-
- Real-time
|
42 |
- Verification of recent claims or rumors
|
43 |
RESPONSE FORMAT:
|
44 |
- Lead with key facts
|
@@ -51,7 +51,7 @@ Remember: Your goal is to be the most reliable, up-to-date source of information
|
|
51 |
def query_compound_model(self, query, model, temperature=0.7, custom_system_prompt=None):
|
52 |
"""Query the compound model and return response with tool execution info"""
|
53 |
if not self.client:
|
54 |
-
return
|
55 |
|
56 |
try:
|
57 |
start_time = time.time()
|
@@ -86,14 +86,14 @@ Remember: Your goal is to be the most reliable, up-to-date source of information
|
|
86 |
return response_content, tool_info, response_time
|
87 |
|
88 |
except Exception as e:
|
89 |
-
return f
|
90 |
|
91 |
def format_tool_info(self, executed_tools):
|
92 |
"""Format executed tools information for display"""
|
93 |
if not executed_tools:
|
94 |
-
return
|
95 |
|
96 |
-
tool_info =
|
97 |
for i, tool in enumerate(executed_tools, 1):
|
98 |
try:
|
99 |
if hasattr(tool, 'name'):
|
@@ -105,64 +105,64 @@ Remember: Your goal is to be the most reliable, up-to-date source of information
|
|
105 |
else:
|
106 |
tool_name = str(tool)
|
107 |
|
108 |
-
tool_info += f
|
109 |
|
110 |
if hasattr(tool, 'parameters'):
|
111 |
params = tool.parameters
|
112 |
if isinstance(params, dict):
|
113 |
-
tool_info +=
|
114 |
for key, value in params.items():
|
115 |
-
tool_info += f
|
116 |
-
tool_info +=
|
117 |
elif hasattr(tool, 'input'):
|
118 |
-
tool_info += f
|
119 |
-
tool_info +=
|
120 |
-
|
121 |
-
except Exception:
|
122 |
-
tool_info += f
|
123 |
|
124 |
-
tool_info +=
|
125 |
return tool_info
|
126 |
|
127 |
def get_example_queries(self):
|
128 |
"""Return categorized example queries"""
|
129 |
return {
|
130 |
-
"
|
131 |
"What are the top 3 news stories today?",
|
132 |
"Latest developments in AI technology this week",
|
133 |
"Recent political events in the United States",
|
134 |
"Breaking news about climate change",
|
135 |
"What happened in the stock market today?"
|
136 |
],
|
137 |
-
"
|
138 |
"Current price of Bitcoin",
|
139 |
"Tesla stock price today",
|
140 |
"How is the S&P 500 performing today?",
|
141 |
"Latest cryptocurrency market trends",
|
142 |
"What's the current inflation rate?"
|
143 |
],
|
144 |
-
"
|
145 |
"Current weather in New York City",
|
146 |
"Weather forecast for London this week",
|
147 |
"Is it going to rain in San Francisco today?",
|
148 |
"Temperature in Tokyo right now",
|
149 |
"Weather conditions in Sydney"
|
150 |
],
|
151 |
-
"
|
152 |
"Latest breakthroughs in fusion energy",
|
153 |
"Recent discoveries in space exploration",
|
154 |
"New developments in quantum computing",
|
155 |
"Latest medical research findings",
|
156 |
"Recent advances in renewable energy"
|
157 |
],
|
158 |
-
"
|
159 |
"Latest football match results",
|
160 |
"Who won the recent tennis tournament?",
|
161 |
"Box office numbers for this weekend",
|
162 |
"Latest movie releases this month",
|
163 |
"Recent celebrity news"
|
164 |
],
|
165 |
-
"
|
166 |
"Is it true that the Earth's population reached 8 billion?",
|
167 |
"Verify: Did company X announce layoffs recently?",
|
168 |
"Check if the recent earthquake in Turkey was magnitude 7+",
|
@@ -174,12 +174,12 @@ Remember: Your goal is to be the most reliable, up-to-date source of information
|
|
174 |
def get_custom_prompt_examples(self):
|
175 |
"""Return custom system prompt examples"""
|
176 |
return {
|
177 |
-
"
|
178 |
-
"
|
179 |
-
"
|
180 |
-
"
|
181 |
-
"
|
182 |
-
"
|
183 |
}
|
184 |
|
185 |
def create_interface():
|
@@ -188,206 +188,200 @@ def create_interface():
|
|
188 |
custom_css = """
|
189 |
<style>
|
190 |
.gradio-container {
|
191 |
-
max-width:
|
192 |
margin: 0 auto;
|
193 |
background: #f5f7fa;
|
194 |
-
min-height: 100vh;
|
195 |
font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
|
196 |
-
|
197 |
}
|
198 |
|
199 |
-
.
|
200 |
-
background:
|
201 |
padding: 2rem;
|
202 |
border-radius: 12px;
|
203 |
-
margin: 2rem 1rem;
|
204 |
text-align: center;
|
205 |
-
box-shadow: 0
|
|
|
206 |
}
|
207 |
|
208 |
-
.
|
209 |
font-size: 2rem;
|
210 |
-
|
211 |
margin: 0;
|
212 |
-
color: #111827;
|
213 |
}
|
214 |
|
215 |
-
.
|
216 |
font-size: 1rem;
|
217 |
-
color: #
|
218 |
margin: 0.5rem 0 0;
|
219 |
}
|
220 |
|
221 |
-
.
|
222 |
-
background:
|
223 |
border-radius: 12px;
|
224 |
padding: 1.5rem;
|
225 |
-
margin:
|
226 |
box-shadow: 0 2px 8px rgba(0,0,0,0.05);
|
227 |
-
|
228 |
}
|
229 |
|
230 |
-
.
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
.example-grid {
|
235 |
-
display: grid;
|
236 |
-
grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
|
237 |
-
gap: 1rem;
|
238 |
-
margin-top: 1rem;
|
239 |
}
|
240 |
|
241 |
-
.
|
242 |
-
background: #
|
|
|
|
|
243 |
border-radius: 8px;
|
244 |
-
|
245 |
-
cursor: pointer;
|
246 |
-
transition: all 0.2s ease;
|
247 |
-
border: 1px solid #e5e7eb;
|
248 |
}
|
249 |
|
250 |
-
.
|
251 |
-
background: #
|
252 |
-
|
253 |
-
}
|
254 |
-
|
255 |
-
.status-success {
|
256 |
-
background: #ecfdf5;
|
257 |
-
color: #065f46;
|
258 |
padding: 0.75rem;
|
259 |
border-radius: 8px;
|
260 |
-
border:
|
261 |
}
|
262 |
|
263 |
-
.
|
264 |
-
background: #
|
265 |
-
|
266 |
-
padding: 0.75rem;
|
267 |
border-radius: 8px;
|
268 |
-
|
|
|
269 |
}
|
270 |
|
271 |
-
.
|
272 |
-
|
|
|
|
|
|
|
|
|
|
|
273 |
border-radius: 12px;
|
274 |
padding: 1.5rem;
|
275 |
-
margin:
|
276 |
box-shadow: 0 2px 8px rgba(0,0,0,0.05);
|
277 |
}
|
278 |
|
279 |
-
.
|
280 |
-
|
281 |
-
|
282 |
-
|
283 |
-
margin: 1rem 0;
|
284 |
-
border: 1px solid #e5e7eb;
|
285 |
}
|
286 |
|
287 |
-
.
|
288 |
-
|
289 |
-
|
290 |
-
padding: 0.5rem 1rem;
|
291 |
-
border-radius: 9999px;
|
292 |
-
display: inline-block;
|
293 |
-
margin: 0.5rem 0;
|
294 |
-
font-size: 0.875rem;
|
295 |
}
|
296 |
|
297 |
-
.
|
298 |
-
|
299 |
-
color:
|
300 |
-
|
301 |
-
|
302 |
-
margin: 2rem 1rem;
|
303 |
-
text-align: center;
|
304 |
}
|
305 |
|
306 |
-
.
|
307 |
-
|
308 |
-
|
309 |
-
|
|
|
310 |
}
|
311 |
|
312 |
-
.
|
313 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
314 |
}
|
315 |
|
316 |
.prompt-example {
|
317 |
-
background: #
|
318 |
-
border-radius: 8px;
|
319 |
padding: 1rem;
|
|
|
320 |
margin: 0.5rem 0;
|
321 |
cursor: pointer;
|
322 |
-
transition:
|
323 |
-
border: 1px solid #e5e7eb;
|
324 |
}
|
325 |
|
326 |
.prompt-example:hover {
|
327 |
-
background: #
|
328 |
}
|
329 |
|
330 |
-
.prompt-
|
331 |
font-weight: 600;
|
332 |
-
color: #
|
333 |
margin-bottom: 0.25rem;
|
334 |
}
|
335 |
|
336 |
-
.prompt-
|
337 |
-
font-size: 0.
|
338 |
-
color: #
|
339 |
-
line-height: 1.5;
|
340 |
-
}
|
341 |
-
|
342 |
-
.gr-button {
|
343 |
-
border-radius: 8px !important;
|
344 |
-
padding: 0.75rem 1.5rem !important;
|
345 |
-
font-weight: 500 !important;
|
346 |
-
}
|
347 |
-
|
348 |
-
.gr-button-primary {
|
349 |
-
background: #2563eb !important;
|
350 |
-
color: white !important;
|
351 |
}
|
352 |
|
353 |
-
.
|
354 |
-
background: #
|
|
|
|
|
|
|
|
|
|
|
355 |
}
|
356 |
|
357 |
-
.
|
358 |
-
|
359 |
-
|
|
|
360 |
}
|
361 |
|
362 |
-
.
|
363 |
-
|
|
|
364 |
}
|
365 |
|
366 |
-
.gr-
|
367 |
-
|
368 |
-
|
|
|
|
|
|
|
|
|
369 |
}
|
370 |
|
371 |
-
.gr-
|
372 |
-
|
373 |
-
|
|
|
|
|
|
|
|
|
374 |
}
|
375 |
</style>
|
376 |
"""
|
377 |
|
378 |
def validate_api_key(api_key):
|
379 |
if not api_key or api_key.strip() == "":
|
380 |
-
return
|
381 |
|
382 |
success, message = fact_checker.initialize_client(api_key.strip())
|
383 |
return message, success
|
384 |
|
385 |
def process_query(query, model, temperature, api_key, system_prompt):
|
386 |
if not api_key or api_key.strip() == "":
|
387 |
-
return
|
388 |
|
389 |
if not query or query.strip() == "":
|
390 |
-
return
|
391 |
|
392 |
if not fact_checker.client:
|
393 |
success, message = fact_checker.initialize_client(api_key.strip())
|
@@ -400,17 +394,16 @@ def create_interface():
|
|
400 |
|
401 |
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
402 |
formatted_response = f"""
|
403 |
-
<div class=
|
404 |
-
<
|
405 |
<p>{query}</p>
|
406 |
-
<
|
407 |
-
<
|
408 |
-
<
|
409 |
-
<p><em>Generated at {timestamp} in {response_time}s</em></p>
|
410 |
</div>
|
411 |
"""
|
412 |
|
413 |
-
return formatted_response, tool_info or "", f
|
414 |
|
415 |
def reset_system_prompt():
|
416 |
return fact_checker.get_system_prompt()
|
@@ -421,74 +414,63 @@ def create_interface():
|
|
421 |
def load_custom_prompt(prompt_text):
|
422 |
return prompt_text
|
423 |
|
424 |
-
with gr.Blocks(title="Real-time Fact Checker
|
|
|
425 |
gr.HTML("""
|
426 |
-
<div class="
|
427 |
<h1>🔍 Real-time Fact Checker</h1>
|
428 |
-
<p>
|
429 |
</div>
|
430 |
""")
|
431 |
|
432 |
with gr.Row():
|
433 |
-
with gr.Column(scale=
|
434 |
with gr.Group():
|
435 |
-
gr.HTML('<div class="
|
436 |
-
gr.Markdown("###
|
437 |
api_key_input = gr.Textbox(
|
438 |
label="Groq API Key",
|
439 |
-
placeholder="Enter your Groq API key
|
440 |
type="password",
|
441 |
-
info="
|
442 |
)
|
443 |
api_status = gr.HTML(
|
444 |
-
value=
|
445 |
)
|
446 |
validate_btn = gr.Button("Validate API Key", variant="secondary")
|
447 |
gr.HTML('</div>')
|
448 |
|
449 |
with gr.Group():
|
450 |
-
gr.HTML('<div class="
|
451 |
-
gr.Markdown("###
|
452 |
-
|
453 |
-
with gr.Accordion("📝 System Prompt Examples", open=False):
|
454 |
-
gr.Markdown("**Click any example to load it as your system prompt:**")
|
455 |
-
custom_prompts = fact_checker.get_custom_prompt_examples()
|
456 |
-
for title, prompt in custom_prompts.items():
|
457 |
-
gr.HTML(f"""
|
458 |
-
<div class="prompt-example" onclick="document.getElementById('system_prompt_input').value = '{prompt}'">
|
459 |
-
<div class="prompt-example-title">{title}</div>
|
460 |
-
<div class="prompt-example-text">{prompt[:100]}...</div>
|
461 |
-
</div>
|
462 |
-
""")
|
463 |
-
|
464 |
-
with gr.Accordion("🔧 System Prompt", open=False):
|
465 |
system_prompt_input = gr.Textbox(
|
466 |
label="System Prompt",
|
467 |
value=fact_checker.get_system_prompt(),
|
468 |
-
lines=
|
469 |
-
info="Customize
|
470 |
-
elem_id="system_prompt_input"
|
471 |
)
|
472 |
reset_prompt_btn = gr.Button("Reset to Default", variant="secondary")
|
473 |
|
474 |
-
gr.Markdown("
|
|
|
475 |
for title, prompt in custom_prompts.items():
|
476 |
-
|
477 |
-
|
478 |
-
|
479 |
-
|
480 |
-
|
|
|
481 |
gr.HTML('</div>')
|
482 |
|
483 |
with gr.Group():
|
484 |
-
gr.HTML('<div class="
|
485 |
-
gr.Markdown("###
|
486 |
query_input = gr.Textbox(
|
487 |
-
label="Ask
|
488 |
-
placeholder="e.g., What
|
489 |
-
lines=
|
490 |
)
|
491 |
-
|
492 |
with gr.Row():
|
493 |
model_choice = gr.Dropdown(
|
494 |
choices=fact_checker.model_options,
|
@@ -502,47 +484,55 @@ def create_interface():
|
|
502 |
step=0.1,
|
503 |
label="Temperature"
|
504 |
)
|
505 |
-
|
506 |
-
submit_btn = gr.Button("🔍 Get Real-time Information", variant="primary")
|
507 |
clear_btn = gr.Button("Clear", variant="secondary")
|
508 |
gr.HTML('</div>')
|
509 |
|
510 |
-
with gr.Column(scale=
|
511 |
with gr.Group():
|
512 |
-
gr.HTML('<div class="
|
513 |
-
gr.Markdown("###
|
514 |
-
gr.Markdown("Click any example to load it:")
|
515 |
-
|
516 |
examples = fact_checker.get_example_queries()
|
517 |
-
|
518 |
-
|
519 |
-
|
520 |
-
|
521 |
-
|
522 |
-
fn=lambda q=query: q,
|
523 |
-
outputs=[query_input]
|
524 |
-
)
|
525 |
gr.HTML('</div>')
|
526 |
|
527 |
-
gr.HTML('<div class="
|
528 |
-
gr.Markdown("###
|
529 |
-
|
530 |
with gr.Row():
|
531 |
-
with gr.Column(scale=
|
532 |
response_output = gr.HTML(
|
533 |
-
value=
|
534 |
)
|
535 |
-
|
536 |
-
with gr.Column(scale=1):
|
537 |
tool_info_output = gr.HTML(
|
538 |
-
value=
|
539 |
)
|
540 |
-
|
541 |
performance_output = gr.HTML(
|
542 |
value=""
|
543 |
)
|
544 |
gr.HTML('</div>')
|
545 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
546 |
validate_btn.click(
|
547 |
fn=validate_api_key,
|
548 |
inputs=[api_key_input],
|
@@ -561,29 +551,9 @@ def create_interface():
|
|
561 |
)
|
562 |
|
563 |
clear_btn.click(
|
564 |
-
fn=lambda: ("", '<div class=
|
565 |
-
'<div class="tool-info"><em>Tool execution details will appear here...</em></div>', ""),
|
566 |
outputs=[query_input, response_output, tool_info_output, performance_output]
|
567 |
)
|
568 |
-
|
569 |
-
gr.HTML("""
|
570 |
-
<div class="footer-section">
|
571 |
-
<h3>🔗 Useful Links</h3>
|
572 |
-
<p>
|
573 |
-
<a href="https://console.groq.com/" target="_blank">Groq Console</a> - Get your free API key<br>
|
574 |
-
<a href="https://console.groq.com/docs/quickstart" target="_blank">Groq Documentation</a> - Learn more about Groq models<br>
|
575 |
-
<a href="https://console.groq.com/docs/models" target="_blank">Compound Models Info</a> - Details about compound models
|
576 |
-
</p>
|
577 |
-
<h3>💡 Tips</h3>
|
578 |
-
<ul style="text-align: left; display: inline-block;">
|
579 |
-
<li>Compound models use web search for real-time information</li>
|
580 |
-
<li>Use temperature 0.1 for factual queries, 0.7-0.9 for creative ones</li>
|
581 |
-
<li>compound-beta: more capable | compound-beta-mini: faster</li>
|
582 |
-
<li>Customize system prompts for specialized responses</li>
|
583 |
-
<li>Check Tool Execution Info for web search usage</li>
|
584 |
-
</ul>
|
585 |
-
</div>
|
586 |
-
""")
|
587 |
|
588 |
return demo
|
589 |
|
|
|
14 |
"""Initialize Groq client with API key"""
|
15 |
try:
|
16 |
self.client = Groq(api_key=api_key)
|
17 |
+
return True, "<div class='status success'>✅ API Key validated successfully!</div>"
|
18 |
except Exception as e:
|
19 |
+
return False, f"<div class='status error'>❌ Error initializing client: {str(e)}</div>"
|
20 |
|
21 |
def get_system_prompt(self):
|
22 |
"""Get the system prompt for consistent behavior"""
|
|
|
38 |
- Weather conditions or forecasts
|
39 |
- Recent scientific discoveries or research
|
40 |
- Current political developments
|
41 |
+
- Real-time statics or data
|
42 |
- Verification of recent claims or rumors
|
43 |
RESPONSE FORMAT:
|
44 |
- Lead with key facts
|
|
|
51 |
def query_compound_model(self, query, model, temperature=0.7, custom_system_prompt=None):
|
52 |
"""Query the compound model and return response with tool execution info"""
|
53 |
if not self.client:
|
54 |
+
return "<div class='status error'>❌ Please set a valid API key first.</div>", None, None
|
55 |
|
56 |
try:
|
57 |
start_time = time.time()
|
|
|
86 |
return response_content, tool_info, response_time
|
87 |
|
88 |
except Exception as e:
|
89 |
+
return f"<div class='status error'>❌ Error querying model: {str(e)}</div>", None, None
|
90 |
|
91 |
def format_tool_info(self, executed_tools):
|
92 |
"""Format executed tools information for display"""
|
93 |
if not executed_tools:
|
94 |
+
return "<div class='tool-info'><strong>Tools Used:</strong> None (Used existing knowledge)</div>"
|
95 |
|
96 |
+
tool_info = "<div class='tool-info'><strong>Tools Used:</strong><ul>"
|
97 |
for i, tool in enumerate(executed_tools, 1):
|
98 |
try:
|
99 |
if hasattr(tool, 'name'):
|
|
|
105 |
else:
|
106 |
tool_name = str(tool)
|
107 |
|
108 |
+
tool_info += f"<li>{i}. <strong>{tool_name}</strong>"
|
109 |
|
110 |
if hasattr(tool, 'parameters'):
|
111 |
params = tool.parameters
|
112 |
if isinstance(params, dict):
|
113 |
+
tool_info += "<ul>"
|
114 |
for key, value in params.items():
|
115 |
+
tool_info += f"<li>{key}: {value}</li>"
|
116 |
+
tool_info += "</ul>"
|
117 |
elif hasattr(tool, 'input'):
|
118 |
+
tool_info += f"<ul><li>Input: {tool.input}</li></ul>"
|
119 |
+
tool_info += "</li>"
|
120 |
+
|
121 |
+
except Exception as e:
|
122 |
+
tool_info += f"<li>{i}. <strong>Tool {i}</strong> (Error parsing details)</li>"
|
123 |
|
124 |
+
tool_info += "</ul></div>"
|
125 |
return tool_info
|
126 |
|
127 |
def get_example_queries(self):
|
128 |
"""Return categorized example queries"""
|
129 |
return {
|
130 |
+
"Latest News": [
|
131 |
"What are the top 3 news stories today?",
|
132 |
"Latest developments in AI technology this week",
|
133 |
"Recent political events in the United States",
|
134 |
"Breaking news about climate change",
|
135 |
"What happened in the stock market today?"
|
136 |
],
|
137 |
+
"Financial Data": [
|
138 |
"Current price of Bitcoin",
|
139 |
"Tesla stock price today",
|
140 |
"How is the S&P 500 performing today?",
|
141 |
"Latest cryptocurrency market trends",
|
142 |
"What's the current inflation rate?"
|
143 |
],
|
144 |
+
"Weather Updates": [
|
145 |
"Current weather in New York City",
|
146 |
"Weather forecast for London this week",
|
147 |
"Is it going to rain in San Francisco today?",
|
148 |
"Temperature in Tokyo right now",
|
149 |
"Weather conditions in Sydney"
|
150 |
],
|
151 |
+
"Science & Technology": [
|
152 |
"Latest breakthroughs in fusion energy",
|
153 |
"Recent discoveries in space exploration",
|
154 |
"New developments in quantum computing",
|
155 |
"Latest medical research findings",
|
156 |
"Recent advances in renewable energy"
|
157 |
],
|
158 |
+
"Sports & Entertainment": [
|
159 |
"Latest football match results",
|
160 |
"Who won the recent tennis tournament?",
|
161 |
"Box office numbers for this weekend",
|
162 |
"Latest movie releases this month",
|
163 |
"Recent celebrity news"
|
164 |
],
|
165 |
+
"Fact Checking": [
|
166 |
"Is it true that the Earth's population reached 8 billion?",
|
167 |
"Verify: Did company X announce layoffs recently?",
|
168 |
"Check if the recent earthquake in Turkey was magnitude 7+",
|
|
|
174 |
def get_custom_prompt_examples(self):
|
175 |
"""Return custom system prompt examples"""
|
176 |
return {
|
177 |
+
"Fact-Checker": "You are a fact-checker. Always verify claims with multiple sources and clearly indicate confidence levels in your assessments. Use phrases like 'highly confident', 'moderately confident', or 'requires verification' when presenting information.",
|
178 |
+
"News Analyst": "You are a news analyst. Focus on providing balanced, unbiased reporting with multiple perspectives on current events. Always present different viewpoints and avoid partisan language.",
|
179 |
+
"Financial Advisor": "You are a financial advisor. Provide accurate market data with context about trends and implications for investors. Always include disclaimers about market risks and the importance of professional financial advice.",
|
180 |
+
"Research Assistant": "You are a research assistant specializing in scientific and technical information. Provide detailed, evidence-based responses with proper context about methodology and limitations of studies.",
|
181 |
+
"Global News Correspondent": "You are a global news correspondent. Focus on international events and their interconnections. Provide cultural context and explain how events in one region might affect others.",
|
182 |
+
"Market Analyst": "You are a market analyst. Provide detailed financial analysis including technical indicators, market sentiment, and economic factors affecting price movements."
|
183 |
}
|
184 |
|
185 |
def create_interface():
|
|
|
188 |
custom_css = """
|
189 |
<style>
|
190 |
.gradio-container {
|
191 |
+
max-width: 1200px;
|
192 |
margin: 0 auto;
|
193 |
background: #f5f7fa;
|
|
|
194 |
font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
|
195 |
+
padding: 20px;
|
196 |
}
|
197 |
|
198 |
+
.header {
|
199 |
+
background: #ffffff;
|
200 |
padding: 2rem;
|
201 |
border-radius: 12px;
|
|
|
202 |
text-align: center;
|
203 |
+
box-shadow: 0 2px 8px rgba(0,0,0,0.05);
|
204 |
+
margin-bottom: 2rem;
|
205 |
}
|
206 |
|
207 |
+
.header h1 {
|
208 |
font-size: 2rem;
|
209 |
+
color: #1a202c;
|
210 |
margin: 0;
|
|
|
211 |
}
|
212 |
|
213 |
+
.header p {
|
214 |
font-size: 1rem;
|
215 |
+
color: #4a5568;
|
216 |
margin: 0.5rem 0 0;
|
217 |
}
|
218 |
|
219 |
+
.card {
|
220 |
+
background: #ffffff;
|
221 |
border-radius: 12px;
|
222 |
padding: 1.5rem;
|
223 |
+
margin-bottom: 1.5rem;
|
224 |
box-shadow: 0 2px 8px rgba(0,0,0,0.05);
|
225 |
+
border: 1px solid #e2e8f0;
|
226 |
}
|
227 |
|
228 |
+
.card h3 {
|
229 |
+
font-size: 1.25rem;
|
230 |
+
color: #2d3748;
|
231 |
+
margin: 0 0 1rem;
|
|
|
|
|
|
|
|
|
|
|
232 |
}
|
233 |
|
234 |
+
.status.success {
|
235 |
+
background: #e6fffa;
|
236 |
+
color: #2b6cb0;
|
237 |
+
padding: 0.75rem;
|
238 |
border-radius: 8px;
|
239 |
+
border-left: 4px solid #2b6cb0;
|
|
|
|
|
|
|
240 |
}
|
241 |
|
242 |
+
.status.error {
|
243 |
+
background: #fff5f5;
|
244 |
+
color: #c53030;
|
|
|
|
|
|
|
|
|
|
|
245 |
padding: 0.75rem;
|
246 |
border-radius: 8px;
|
247 |
+
border-left: 4px solid #c53030;
|
248 |
}
|
249 |
|
250 |
+
.tool-info {
|
251 |
+
background: #edf2f7;
|
252 |
+
padding: 1rem;
|
|
|
253 |
border-radius: 8px;
|
254 |
+
margin: 1rem 0;
|
255 |
+
color: #2d3748;
|
256 |
}
|
257 |
|
258 |
+
.tool-info ul {
|
259 |
+
margin: 0.5rem 0;
|
260 |
+
padding-left: 1.5rem;
|
261 |
+
}
|
262 |
+
|
263 |
+
.result-card {
|
264 |
+
background: #ffffff;
|
265 |
border-radius: 12px;
|
266 |
padding: 1.5rem;
|
267 |
+
margin: 1.5rem 0;
|
268 |
box-shadow: 0 2px 8px rgba(0,0,0,0.05);
|
269 |
}
|
270 |
|
271 |
+
.result-card h3 {
|
272 |
+
font-size: 1.25rem;
|
273 |
+
color: #2d3748;
|
274 |
+
margin: 0 0 1rem;
|
|
|
|
|
275 |
}
|
276 |
|
277 |
+
.result-content {
|
278 |
+
color: #4a5568;
|
279 |
+
line-height: 1.6;
|
|
|
|
|
|
|
|
|
|
|
280 |
}
|
281 |
|
282 |
+
.performance {
|
283 |
+
font-size: 0.9rem;
|
284 |
+
color: #2b6cb0;
|
285 |
+
margin-top: 1rem;
|
286 |
+
font-style: italic;
|
|
|
|
|
287 |
}
|
288 |
|
289 |
+
.example-grid {
|
290 |
+
display: grid;
|
291 |
+
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
|
292 |
+
gap: 1rem;
|
293 |
+
margin-top: 1rem;
|
294 |
}
|
295 |
|
296 |
+
.example-item {
|
297 |
+
background: #edf2f7;
|
298 |
+
padding: 1rem;
|
299 |
+
border-radius: 8px;
|
300 |
+
cursor: pointer;
|
301 |
+
transition: background 0.2s;
|
302 |
+
}
|
303 |
+
|
304 |
+
.example-item:hover {
|
305 |
+
background: #e2e8f0;
|
306 |
}
|
307 |
|
308 |
.prompt-example {
|
309 |
+
background: #e6fffa;
|
|
|
310 |
padding: 1rem;
|
311 |
+
border-radius: 8px;
|
312 |
margin: 0.5rem 0;
|
313 |
cursor: pointer;
|
314 |
+
transition: background 0.2s;
|
|
|
315 |
}
|
316 |
|
317 |
.prompt-example:hover {
|
318 |
+
background: #b2f5ea;
|
319 |
}
|
320 |
|
321 |
+
.prompt-title {
|
322 |
font-weight: 600;
|
323 |
+
color: #2b6cb0;
|
324 |
margin-bottom: 0.25rem;
|
325 |
}
|
326 |
|
327 |
+
.prompt-text {
|
328 |
+
font-size: 0.9rem;
|
329 |
+
color: #4a5568;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
330 |
}
|
331 |
|
332 |
+
.footer {
|
333 |
+
background: #2d3748;
|
334 |
+
color: #e2e8f0;
|
335 |
+
padding: 2rem;
|
336 |
+
border-radius: 12px;
|
337 |
+
text-align: center;
|
338 |
+
margin-top: 2rem;
|
339 |
}
|
340 |
|
341 |
+
.footer a {
|
342 |
+
color: #63b3ed;
|
343 |
+
text-decoration: none;
|
344 |
+
font-weight: 500;
|
345 |
}
|
346 |
|
347 |
+
.footer a:hover {
|
348 |
+
color: #90cdf4;
|
349 |
+
text-decoration: underline;
|
350 |
}
|
351 |
|
352 |
+
.gr-button-primary {
|
353 |
+
background: #2b6cb0;
|
354 |
+
color: #ffffff;
|
355 |
+
border: none;
|
356 |
+
padding: 0.75rem 1.5rem;
|
357 |
+
border-radius: 8px;
|
358 |
+
font-weight: 500;
|
359 |
}
|
360 |
|
361 |
+
.gr-button-secondary {
|
362 |
+
background: #e2e8f0;
|
363 |
+
color: #2d3748;
|
364 |
+
border: none;
|
365 |
+
padding: 0.5rem 1rem;
|
366 |
+
border-radius: 8px;
|
367 |
+
font-weight: 500;
|
368 |
}
|
369 |
</style>
|
370 |
"""
|
371 |
|
372 |
def validate_api_key(api_key):
|
373 |
if not api_key or api_key.strip() == "":
|
374 |
+
return "<div class='status error'>❌ Please enter a valid API key</div>", False
|
375 |
|
376 |
success, message = fact_checker.initialize_client(api_key.strip())
|
377 |
return message, success
|
378 |
|
379 |
def process_query(query, model, temperature, api_key, system_prompt):
|
380 |
if not api_key or api_key.strip() == "":
|
381 |
+
return "<div class='status error'>❌ Please set your API key first</div>", "", ""
|
382 |
|
383 |
if not query or query.strip() == "":
|
384 |
+
return "<div class='status error'>❌ Please enter a query</div>", "", ""
|
385 |
|
386 |
if not fact_checker.client:
|
387 |
success, message = fact_checker.initialize_client(api_key.strip())
|
|
|
394 |
|
395 |
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
396 |
formatted_response = f"""
|
397 |
+
<div class='result-content'>
|
398 |
+
<h4>Query:</h4>
|
399 |
<p>{query}</p>
|
400 |
+
<h4>Response:</h4>
|
401 |
+
<p>{response}</p>
|
402 |
+
<p class='performance'>Generated at {timestamp} in {response_time}s</p>
|
|
|
403 |
</div>
|
404 |
"""
|
405 |
|
406 |
+
return formatted_response, tool_info or "", f"<div class='performance'>⚡ Response time: {response_time}s</div>"
|
407 |
|
408 |
def reset_system_prompt():
|
409 |
return fact_checker.get_system_prompt()
|
|
|
414 |
def load_custom_prompt(prompt_text):
|
415 |
return prompt_text
|
416 |
|
417 |
+
with gr.Blocks(title="Real-time Fact Checker", css=custom_css) as demo:
|
418 |
+
|
419 |
gr.HTML("""
|
420 |
+
<div class="header">
|
421 |
<h1>🔍 Real-time Fact Checker</h1>
|
422 |
+
<p>Instantly verify facts and get the latest news with Groq's Compound Models</p>
|
423 |
</div>
|
424 |
""")
|
425 |
|
426 |
with gr.Row():
|
427 |
+
with gr.Column(scale=3):
|
428 |
with gr.Group():
|
429 |
+
gr.HTML('<div class="card">')
|
430 |
+
gr.Markdown("### API Configuration")
|
431 |
api_key_input = gr.Textbox(
|
432 |
label="Groq API Key",
|
433 |
+
placeholder="Enter your Groq API key...",
|
434 |
type="password",
|
435 |
+
info="Obtain your free API key from https://console.groq.com/"
|
436 |
)
|
437 |
api_status = gr.HTML(
|
438 |
+
value="<div class='status error'>⚠️ Please enter your API key</div>"
|
439 |
)
|
440 |
validate_btn = gr.Button("Validate API Key", variant="secondary")
|
441 |
gr.HTML('</div>')
|
442 |
|
443 |
with gr.Group():
|
444 |
+
gr.HTML('<div class="card">')
|
445 |
+
gr.Markdown("### System Prompt")
|
446 |
+
with gr.Accordion("Customize System Prompt", open=False):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
447 |
system_prompt_input = gr.Textbox(
|
448 |
label="System Prompt",
|
449 |
value=fact_checker.get_system_prompt(),
|
450 |
+
lines=6,
|
451 |
+
info="Customize the AI's behavior"
|
|
|
452 |
)
|
453 |
reset_prompt_btn = gr.Button("Reset to Default", variant="secondary")
|
454 |
|
455 |
+
gr.Markdown("#### Prompt Examples")
|
456 |
+
custom_prompts = fact_checker.get_custom_prompt_examples()
|
457 |
for title, prompt in custom_prompts.items():
|
458 |
+
gr.HTML(f"""
|
459 |
+
<div class="prompt-example" onclick="document.getElementById('system_prompt_input').value = '{prompt}'">
|
460 |
+
<div class="prompt-title">{title}</div>
|
461 |
+
<div class="prompt-text">{prompt[:80]}...</div>
|
462 |
+
</div>
|
463 |
+
""")
|
464 |
gr.HTML('</div>')
|
465 |
|
466 |
with gr.Group():
|
467 |
+
gr.HTML('<div class="card">')
|
468 |
+
gr.Markdown("### Your Query")
|
469 |
query_input = gr.Textbox(
|
470 |
+
label="Ask a Question",
|
471 |
+
placeholder="e.g., What's the latest news on AI developments?",
|
472 |
+
lines=3
|
473 |
)
|
|
|
474 |
with gr.Row():
|
475 |
model_choice = gr.Dropdown(
|
476 |
choices=fact_checker.model_options,
|
|
|
484 |
step=0.1,
|
485 |
label="Temperature"
|
486 |
)
|
487 |
+
submit_btn = gr.Button("🔍 Get Answer", variant="primary")
|
|
|
488 |
clear_btn = gr.Button("Clear", variant="secondary")
|
489 |
gr.HTML('</div>')
|
490 |
|
491 |
+
with gr.Column(scale=2):
|
492 |
with gr.Group():
|
493 |
+
gr.HTML('<div class="card">')
|
494 |
+
gr.Markdown("### Example Queries")
|
|
|
|
|
495 |
examples = fact_checker.get_example_queries()
|
496 |
+
with gr.Tabs():
|
497 |
+
for category, queries in examples.items():
|
498 |
+
with gr.Tab(category):
|
499 |
+
for query in queries:
|
500 |
+
gr.HTML(f'<div class="example-item" onclick="document.getElementById(\'query_input\').value = \'{query}\'">{query}</div>')
|
|
|
|
|
|
|
501 |
gr.HTML('</div>')
|
502 |
|
503 |
+
gr.HTML('<div class="result-card">')
|
504 |
+
gr.Markdown("### Results")
|
|
|
505 |
with gr.Row():
|
506 |
+
with gr.Column(scale=3):
|
507 |
response_output = gr.HTML(
|
508 |
+
value="<div class='result-content'>Enter a query to see results...</div>"
|
509 |
)
|
510 |
+
with gr.Column(scale=2):
|
|
|
511 |
tool_info_output = gr.HTML(
|
512 |
+
value="<div class='tool-info'>Tool execution details will appear here...</div>"
|
513 |
)
|
|
|
514 |
performance_output = gr.HTML(
|
515 |
value=""
|
516 |
)
|
517 |
gr.HTML('</div>')
|
518 |
|
519 |
+
gr.HTML("""
|
520 |
+
<div class="footer">
|
521 |
+
<h3>Links</h3>
|
522 |
+
<p>
|
523 |
+
<a href="https://console.groq.com/" target="_blank">Groq Console</a> |
|
524 |
+
<a href="https://console.groq.com/docs/quickstart" target="_blank">Documentation</a> |
|
525 |
+
<a href="https://console.groq.com/docs/models" target="_blank">Models Info</a>
|
526 |
+
</p>
|
527 |
+
<h3>Tips</h3>
|
528 |
+
<ul style="text-align: left; display: inline-block;">
|
529 |
+
<li>Use compound-beta for detailed responses</li>
|
530 |
+
<li>Adjust temperature for creative or factual responses</li>
|
531 |
+
<li>Check tool execution info for source transparency</li>
|
532 |
+
</ul>
|
533 |
+
</div>
|
534 |
+
""")
|
535 |
+
|
536 |
validate_btn.click(
|
537 |
fn=validate_api_key,
|
538 |
inputs=[api_key_input],
|
|
|
551 |
)
|
552 |
|
553 |
clear_btn.click(
|
554 |
+
fn=lambda: ("", "<div class='result-content'>Enter a query to see results...</div>", "<div class='tool-info'>Tool execution details will appear here...</div>", ""),
|
|
|
555 |
outputs=[query_input, response_output, tool_info_output, performance_output]
|
556 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
557 |
|
558 |
return demo
|
559 |
|