ErNewdev0 commited on
Commit
a31a155
Β·
verified Β·
1 Parent(s): 82139ff

fix: ui and clone repo

Browse files
Files changed (1) hide show
  1. app.py +229 -145
app.py CHANGED
@@ -49,8 +49,6 @@ GEMINI_MODELS = [
49
  "gemini-pro-vision",
50
  ]
51
 
52
- # Help texts
53
-
54
  GITHUB_TOKEN_HELP = """
55
  ### Cara Mendapatkan GitHub Token:
56
 
@@ -96,6 +94,7 @@ Catatan:
96
  - Ideal untuk privasi dan penggunaan offline
97
  """
98
 
 
99
  XAI_API_HELP = """
100
  ### Cara Mendapatkan X.AI (Grok) API Key:
101
 
@@ -281,163 +280,243 @@ class RepoAnalyzer:
281
  def create_ui():
282
  analyzer = RepoAnalyzer()
283
 
284
- with gr.Blocks(title="Repository Chat Analysis") as app:
285
- gr.Markdown(f"""
286
- # πŸ€– Repository Chat Analysis
287
-
288
- πŸ“… Waktu: {CURRENT_TIME}
289
- πŸ‘€ Pengguna: {CURRENT_USER}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
290
  """)
291
 
292
- with gr.Tab("πŸ› οΈ Konfigurasi"):
293
- provider = gr.Radio(
294
- choices=[AIProvider.XAI, AIProvider.GEMINI, AIProvider.OLLAMA],
295
- label="Penyedia AI",
296
- value=AIProvider.XAI
297
- )
298
 
299
- with gr.Group() as api_settings:
300
- with gr.Row():
301
- xai_key = gr.Textbox(
302
- label="X.AI (Grok) API Key",
303
- type="password",
304
- placeholder="Opsional - Klik icon (?) untuk info. Kosongkan untuk gunakan key default",
305
- show_label=True
306
- )
307
- gr.Markdown(XAI_API_HELP)
 
 
 
 
308
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
309
  with gr.Row():
310
- gemini_key = gr.Textbox(
311
- label="Gemini API Key",
312
- type="password",
313
- placeholder="Opsional - Kosongkan untuk gunakan key default",
314
- show_label=True
 
315
  )
316
- gr.Markdown(GEMINI_API_HELP)
317
-
318
- # Model selection based on provider
319
- with gr.Row():
320
- model_dropdown = gr.Dropdown(
321
- label="Model AI",
322
- choices=XAI_MODELS,
323
- value="grok-2-latest",
324
- interactive=True
325
- )
326
 
327
- def update_model_list(provider_choice):
328
- if provider_choice == AIProvider.XAI:
329
- return gr.Dropdown(choices=XAI_MODELS, value="grok-2-latest")
330
- elif provider_choice == AIProvider.GEMINI:
331
- return gr.Dropdown(choices=GEMINI_MODELS, value="gemini-pro")
332
- else: # OLLAMA
333
- return gr.Dropdown(choices=OLLAMA_MODELS, value="llama2")
334
-
335
- provider.change(
336
- fn=update_model_list,
337
- inputs=[provider],
338
- outputs=[model_dropdown]
339
- )
340
-
341
- with gr.Tab("πŸ“Š Analisis Repository"):
342
- with gr.Row():
343
- repo_url = gr.Textbox(
344
- label="URL Repository GitHub",
345
- placeholder="Contoh: https://github.com/username/repository"
 
 
 
 
 
 
 
 
 
 
346
  )
347
- with gr.Column():
348
- github_token = gr.Textbox(
349
- label="Token GitHub",
350
- type="password",
351
- placeholder="Klik icon (?) untuk panduan mendapatkan token"
352
- )
353
- gr.Markdown(GITHUB_TOKEN_HELP)
354
- branch = gr.Textbox(
355
- label="Branch (opsional)",
356
- placeholder="main"
357
  )
358
-
359
- clone_button = gr.Button("πŸ”„ Clone Repository", variant="primary")
360
- clone_status = gr.Markdown(
361
- label="Status Repository",
362
- )
363
-
364
- gr.Markdown("""
365
- ### πŸ’‘ Contoh Pertanyaan:
366
- - "Jelaskan struktur utama dari repository ini"
367
- - "Apa saja fitur-fitur utama dalam kode ini?"
368
- - "Bagaimana cara memperbaiki [masalah specific] di repository ini?"
369
- - "Tolong analisis kualitas kode di file [nama file]"
370
- """)
371
-
372
- with gr.Row():
373
- chat_input = gr.Textbox(
374
- label="πŸ’­ Tanyakan tentang Repository",
375
- placeholder="Ketik pertanyaan Anda di sini...",
376
- lines=3
377
  )
378
- send_button = gr.Button("πŸ“€ Kirim", variant="primary")
379
-
380
- chat_history = gr.Chatbot(
381
- label="πŸ“ Riwayat Chat",
382
- height=500,
383
- show_label=True
384
- )
385
 
386
- async def handle_chat(message, history, provider_choice, model_name, xai_key, gemini_key):
387
- if not analyzer.current_repo:
388
- yield history + [[message, "⚠️ Mohon clone repository terlebih dahulu sebelum mengajukan pertanyaan."]]
389
- return
 
 
 
390
 
391
- history = history or []
392
- history.append([message, ""])
 
 
 
 
 
 
 
 
 
 
 
393
 
394
- full_response = ""
395
- if provider_choice == AIProvider.XAI:
396
- async for chunk in analyzer.stream_xai_response(message, xai_key, model_name):
397
- full_response += chunk
398
- history[-1][1] = full_response
399
- yield history
400
- elif provider_choice == AIProvider.GEMINI:
401
- async for chunk in analyzer.stream_gemini_response(message, gemini_key or DEFAULT_GEMINI_KEY):
402
- full_response += chunk
403
- history[-1][1] = full_response
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
404
  yield history
405
- else: # OLLAMA
406
- response = analyze_with_ollama(model_name, message)
407
- history[-1][1] = response
408
- yield history
409
-
410
- send_button.click(
411
- fn=handle_chat,
412
- inputs=[
413
- chat_input,
414
- chat_history,
415
- provider,
416
- model_dropdown,
417
- xai_key,
418
- gemini_key
419
- ],
420
- outputs=chat_history
421
- ).then(
422
- fn=lambda: gr.update(value=""),
423
- outputs=chat_input
424
- )
425
 
426
- chat_input.submit(
427
- fn=handle_chat,
428
- inputs=[
429
- chat_input,
430
- chat_history,
431
- provider,
432
- model_dropdown,
433
- xai_key,
434
- gemini_key
435
- ],
436
- outputs=chat_history
437
- ).then(
438
- fn=lambda: gr.update(value=""),
439
- outputs=chat_input
440
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
441
 
442
  return app
443
 
@@ -449,4 +528,9 @@ if __name__ == "__main__":
449
  """)
450
 
451
  app = create_ui()
452
- app.launch(share=True)
 
 
 
 
 
 
49
  "gemini-pro-vision",
50
  ]
51
 
 
 
52
  GITHUB_TOKEN_HELP = """
53
  ### Cara Mendapatkan GitHub Token:
54
 
 
94
  - Ideal untuk privasi dan penggunaan offline
95
  """
96
 
97
+ # Help texts
98
  XAI_API_HELP = """
99
  ### Cara Mendapatkan X.AI (Grok) API Key:
100
 
 
280
  def create_ui():
281
  analyzer = RepoAnalyzer()
282
 
283
+ with gr.Blocks(title="Repository Chat Analysis", theme=gr.themes.Soft()) as app:
284
+ # Add CSS for better mobile responsiveness
285
+ gr.Markdown("""
286
+ <style>
287
+ .container {
288
+ max-width: 100% !important;
289
+ padding: 1rem;
290
+ }
291
+ .mobile-full {
292
+ width: 100% !important;
293
+ }
294
+ @media (max-width: 768px) {
295
+ .gr-form {
296
+ flex-direction: column !important;
297
+ }
298
+ .gr-box {
299
+ margin: 0.5rem 0 !important;
300
+ }
301
+ }
302
+ </style>
303
  """)
304
 
305
+ with gr.Row(elem_classes="container"):
306
+ gr.Markdown(f"""
307
+ # πŸ€– Repository Chat Analysis
 
 
 
308
 
309
+ πŸ“… Waktu: {CURRENT_TIME}
310
+ πŸ‘€ Pengguna: {CURRENT_USER}
311
+ """)
312
+
313
+ with gr.Tabs() as tabs:
314
+ with gr.TabItem("πŸ› οΈ Konfigurasi", elem_classes="mobile-full"):
315
+ provider = gr.Radio(
316
+ choices=[AIProvider.XAI, AIProvider.GEMINI, AIProvider.OLLAMA],
317
+ label="Penyedia AI",
318
+ value=AIProvider.XAI,
319
+ interactive=True,
320
+ elem_classes="mobile-full"
321
+ )
322
 
323
+ with gr.Group() as api_settings:
324
+ with gr.Box():
325
+ with gr.Row():
326
+ xai_key = gr.Textbox(
327
+ label="X.AI (Grok) API Key",
328
+ type="password",
329
+ placeholder="Opsional - Klik icon (?) untuk info",
330
+ show_label=True,
331
+ scale=3
332
+ )
333
+ with gr.Column(scale=1):
334
+ gr.Markdown(XAI_API_HELP)
335
+
336
+ with gr.Box():
337
+ with gr.Row():
338
+ gemini_key = gr.Textbox(
339
+ label="Gemini API Key",
340
+ type="password",
341
+ placeholder="Opsional - Kosongkan untuk gunakan key default",
342
+ show_label=True,
343
+ scale=3
344
+ )
345
+ with gr.Column(scale=1):
346
+ gr.Markdown(GEMINI_API_HELP)
347
+
348
+ # Model selection with better layout
349
  with gr.Row():
350
+ model_dropdown = gr.Dropdown(
351
+ label="Model AI",
352
+ choices=XAI_MODELS,
353
+ value="grok-2-latest",
354
+ interactive=True,
355
+ elem_classes="mobile-full"
356
  )
 
 
 
 
 
 
 
 
 
 
357
 
358
+ with gr.TabItem("πŸ“Š Analisis Repository", elem_classes="mobile-full"):
359
+ with gr.Box():
360
+ with gr.Row():
361
+ repo_url = gr.Textbox(
362
+ label="URL Repository GitHub",
363
+ placeholder="https://github.com/username/repository",
364
+ elem_classes="mobile-full"
365
+ )
366
+
367
+ with gr.Row():
368
+ with gr.Column(scale=2):
369
+ github_token = gr.Textbox(
370
+ label="Token GitHub",
371
+ type="password",
372
+ placeholder="Klik icon (?) untuk panduan",
373
+ elem_classes="mobile-full"
374
+ )
375
+ with gr.Column(scale=1):
376
+ branch = gr.Textbox(
377
+ label="Branch (opsional)",
378
+ placeholder="main",
379
+ elem_classes="mobile-full"
380
+ )
381
+
382
+ # Fix clone repository functionality
383
+ clone_button = gr.Button(
384
+ "πŸ”„ Clone Repository",
385
+ variant="primary",
386
+ elem_classes="mobile-full"
387
  )
388
+
389
+ clone_status = gr.Markdown(
390
+ "",
391
+ label="Status Repository",
392
+ elem_classes="mobile-full"
 
 
 
 
 
393
  )
394
+
395
+ def handle_clone(repo_url, github_token, branch):
396
+ if not repo_url:
397
+ return "⚠️ URL repository diperlukan!"
398
+ success, message = analyzer.clone_repository(repo_url, github_token, branch)
399
+ return message
400
+
401
+ # Connect clone button click event
402
+ clone_button.click(
403
+ fn=handle_clone,
404
+ inputs=[repo_url, github_token, branch],
405
+ outputs=clone_status
 
 
 
 
 
 
 
406
  )
 
 
 
 
 
 
 
407
 
408
+ gr.Markdown("""
409
+ ### πŸ’‘ Contoh Pertanyaan:
410
+ - "Jelaskan struktur utama dari repository ini"
411
+ - "Apa saja fitur-fitur utama dalam kode ini?"
412
+ - "Bagaimana cara memperbaiki [masalah specific] di repository ini?"
413
+ - "Tolong analisis kualitas kode di file [nama file]"
414
+ """)
415
 
416
+ # Improved chat interface
417
+ with gr.Box():
418
+ chat_input = gr.Textbox(
419
+ label="πŸ’­ Tanyakan tentang Repository",
420
+ placeholder="Ketik pertanyaan Anda di sini...",
421
+ lines=3,
422
+ elem_classes="mobile-full"
423
+ )
424
+ send_button = gr.Button(
425
+ "πŸ“€ Kirim",
426
+ variant="primary",
427
+ elem_classes="mobile-full"
428
+ )
429
 
430
+ chat_history = gr.Chatbot(
431
+ label="πŸ“ Riwayat Chat",
432
+ height=500,
433
+ show_label=True,
434
+ elem_classes="mobile-full"
435
+ )
436
+
437
+ # Add loading indicator
438
+ with gr.Row():
439
+ loading_indicator = gr.HTML(
440
+ '<div id="loading" style="display:none">Memproses permintaan...</div>'
441
+ )
442
+
443
+ async def handle_chat(message, history, provider_choice, model_name, xai_key, gemini_key):
444
+ if not analyzer.current_repo:
445
+ yield history + [[message, "⚠️ Mohon clone repository terlebih dahulu sebelum mengajukan pertanyaan."]]
446
+ return
447
+
448
+ history = history or []
449
+ history.append([message, ""])
450
+
451
+ try:
452
+ full_response = ""
453
+ if provider_choice == AIProvider.XAI:
454
+ async for chunk in analyzer.stream_xai_response(message, xai_key, model_name):
455
+ full_response += chunk
456
+ history[-1][1] = full_response
457
+ yield history
458
+ elif provider_choice == AIProvider.GEMINI:
459
+ async for chunk in analyzer.stream_gemini_response(message, gemini_key or DEFAULT_GEMINI_KEY):
460
+ full_response += chunk
461
+ history[-1][1] = full_response
462
+ yield history
463
+ else: # OLLAMA
464
+ response = analyze_with_ollama(model_name, message)
465
+ history[-1][1] = response
466
+ yield history
467
+ except Exception as e:
468
+ history[-1][1] = f"⚠️ Error: {str(e)}"
469
  yield history
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
470
 
471
+ # Connect chat events with loading states
472
+ send_event = send_button.click(
473
+ fn=handle_chat,
474
+ inputs=[
475
+ chat_input,
476
+ chat_history,
477
+ provider,
478
+ model_dropdown,
479
+ xai_key,
480
+ gemini_key
481
+ ],
482
+ outputs=chat_history,
483
+ show_progress=True
484
+ ).then(
485
+ fn=lambda: gr.update(value=""),
486
+ outputs=chat_input
487
+ )
488
+
489
+ input_event = chat_input.submit(
490
+ fn=handle_chat,
491
+ inputs=[
492
+ chat_input,
493
+ chat_history,
494
+ provider,
495
+ model_dropdown,
496
+ xai_key,
497
+ gemini_key
498
+ ],
499
+ outputs=chat_history,
500
+ show_progress=True
501
+ ).then(
502
+ fn=lambda: gr.update(value=""),
503
+ outputs=chat_input
504
+ )
505
+
506
+ # Update provider change handler
507
+ def update_model_list(provider_choice):
508
+ if provider_choice == AIProvider.XAI:
509
+ return gr.Dropdown(choices=XAI_MODELS, value="grok-2-latest")
510
+ elif provider_choice == AIProvider.GEMINI:
511
+ return gr.Dropdown(choices=GEMINI_MODELS, value="gemini-1.5-mini")
512
+ else: # OLLAMA
513
+ return gr.Dropdown(choices=OLLAMA_MODELS, value="llama2")
514
+
515
+ provider.change(
516
+ fn=update_model_list,
517
+ inputs=[provider],
518
+ outputs=[model_dropdown]
519
+ )
520
 
521
  return app
522
 
 
528
  """)
529
 
530
  app = create_ui()
531
+ app.launch(
532
+ share=True,
533
+ show_error=True,
534
+ server_name="0.0.0.0",
535
+ server_port=7860
536
+ )