Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -16,38 +16,41 @@ import dotenv
|
|
| 16 |
# Load environment variables
|
| 17 |
dotenv.load_dotenv()
|
| 18 |
|
| 19 |
-
# Metadata
|
| 20 |
-
CURRENT_TIME =
|
| 21 |
-
CURRENT_USER =
|
| 22 |
|
| 23 |
# Default API Keys (fallback if user doesn't provide their own)
|
| 24 |
-
DEFAULT_XAI_KEY = os.getenv(
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
# API settings
|
| 28 |
-
OLLAMA_API = os.environ.get(
|
| 29 |
-
XAI_BASE_URL =
|
| 30 |
|
| 31 |
# Model lists
|
| 32 |
OLLAMA_MODELS = [
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
]
|
| 42 |
|
| 43 |
XAI_MODELS = [
|
| 44 |
-
|
| 45 |
-
|
| 46 |
]
|
| 47 |
|
| 48 |
GEMINI_MODELS = [
|
| 49 |
-
|
| 50 |
-
|
| 51 |
]
|
| 52 |
|
| 53 |
# Help texts
|
|
@@ -112,296 +115,276 @@ Note:
|
|
| 112 |
|
| 113 |
|
| 114 |
class AIProvider:
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
|
| 119 |
|
| 120 |
class RepoAnalyzer:
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 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 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
|
| 301 |
|
| 302 |
analyzer = RepoAnalyzer()
|
| 303 |
|
| 304 |
|
| 305 |
-
async def handle_chat(
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
)
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
await asyncio.sleep(0.05)
|
| 373 |
-
yield history
|
| 374 |
-
|
| 375 |
-
else: # OLLAMA
|
| 376 |
-
response = analyze_with_ollama(model_name, enhanced_message)
|
| 377 |
-
words = response.split()
|
| 378 |
-
for i in range(len(words)):
|
| 379 |
-
full_response = ' '.join(words[: i + 1])
|
| 380 |
-
formatted_response = format_response(full_response)
|
| 381 |
-
history[-1]['content'] = (
|
| 382 |
-
f'<div class="assistant-message">{formatted_response}</div>'
|
| 383 |
-
)
|
| 384 |
-
await asyncio.sleep(0.05)
|
| 385 |
-
yield history
|
| 386 |
-
|
| 387 |
-
except Exception as e:
|
| 388 |
-
error_msg = f'β οΈ Error: {str(e)}'
|
| 389 |
-
history[-1]['content'] = f'<div class="assistant-message">{error_msg}</div>'
|
| 390 |
-
yield history
|
| 391 |
-
|
| 392 |
|
| 393 |
def format_response(text):
|
| 394 |
-
|
| 395 |
-
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
|
| 401 |
-
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
|
| 405 |
<div class="code-block">
|
| 406 |
<div class="code-header">
|
| 407 |
<span class="code-title">{language}</span>
|
|
@@ -411,22 +394,22 @@ def format_response(text):
|
|
| 411 |
</div>
|
| 412 |
'''
|
| 413 |
|
| 414 |
-
|
| 415 |
-
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
| 421 |
|
| 422 |
|
| 423 |
def create_ui():
|
| 424 |
-
|
| 425 |
-
|
| 426 |
|
| 427 |
-
|
| 428 |
-
|
| 429 |
-
|
| 430 |
<style>
|
| 431 |
/* General Styles */
|
| 432 |
.container { max-width: 100% !important; padding: 1rem; }
|
|
@@ -607,40 +590,38 @@ def create_ui():
|
|
| 607 |
</script>
|
| 608 |
""")
|
| 609 |
|
| 610 |
-
|
| 611 |
-
|
| 612 |
-
|
| 613 |
# AI Github Repository Chat
|
| 614 |
|
| 615 |
<div class="header-info">
|
| 616 |
<span class="timestamp">Current Date and Time (UTC - YYYY-MM-DD HH:MM:SS formatted): {current_time}</span>
|
| 617 |
-
<span class="user-info">Current User's Login: {CURRENT_USER}</span>
|
| 618 |
</div>
|
| 619 |
""")
|
| 620 |
|
| 621 |
-
|
| 622 |
-
|
| 623 |
-
|
| 624 |
-
|
| 625 |
-
|
| 626 |
-
|
| 627 |
-
|
| 628 |
-
|
| 629 |
-
|
| 630 |
-
|
| 631 |
-
|
| 632 |
-
|
| 633 |
-
|
| 634 |
-
|
| 635 |
-
|
| 636 |
-
|
| 637 |
-
|
| 638 |
-
|
| 639 |
-
|
| 640 |
-
|
| 641 |
-
|
| 642 |
-
|
| 643 |
-
gr.Markdown("""
|
| 644 |
<details>
|
| 645 |
<summary>β Cara Mendapatkan X.AI API Key</summary>
|
| 646 |
<div class="help-content">
|
|
@@ -659,17 +640,17 @@ def create_ui():
|
|
| 659 |
</details>
|
| 660 |
""")
|
| 661 |
|
| 662 |
-
|
| 663 |
-
|
| 664 |
-
|
| 665 |
-
|
| 666 |
-
|
| 667 |
-
|
| 668 |
-
|
| 669 |
-
|
| 670 |
-
|
| 671 |
-
|
| 672 |
-
|
| 673 |
<details>
|
| 674 |
<summary>β Cara Mendapatkan Gemini API Key</summary>
|
| 675 |
<div class="help-content">
|
|
@@ -689,35 +670,35 @@ def create_ui():
|
|
| 689 |
</details>
|
| 690 |
""")
|
| 691 |
|
| 692 |
-
|
| 693 |
-
|
| 694 |
-
|
| 695 |
-
|
| 696 |
-
|
| 697 |
-
|
| 698 |
-
|
| 699 |
-
|
| 700 |
-
|
| 701 |
-
|
| 702 |
-
|
| 703 |
-
|
| 704 |
-
|
| 705 |
-
|
| 706 |
-
|
| 707 |
-
|
| 708 |
-
|
| 709 |
-
|
| 710 |
-
|
| 711 |
-
|
| 712 |
-
|
| 713 |
-
|
| 714 |
-
|
| 715 |
-
|
| 716 |
-
|
| 717 |
-
|
| 718 |
-
|
| 719 |
-
|
| 720 |
-
|
| 721 |
<details>
|
| 722 |
<summary>β Cara Mendapatkan GitHub Token</summary>
|
| 723 |
<div class="help-content">
|
|
@@ -737,221 +718,370 @@ def create_ui():
|
|
| 737 |
</div>
|
| 738 |
</details>
|
| 739 |
""")
|
| 740 |
-
|
| 741 |
-
|
| 742 |
-
|
| 743 |
-
|
| 744 |
-
|
| 745 |
-
|
| 746 |
-
|
| 747 |
-
|
| 748 |
-
|
| 749 |
-
|
| 750 |
-
|
| 751 |
-
|
| 752 |
-
|
| 753 |
-
|
| 754 |
-
|
| 755 |
-
|
| 756 |
-
|
| 757 |
-
|
| 758 |
-
|
| 759 |
-
|
| 760 |
-
|
| 761 |
-
|
| 762 |
-
|
| 763 |
-
|
| 764 |
-
|
| 765 |
-
|
| 766 |
-
|
| 767 |
-
|
| 768 |
-
|
| 769 |
-
|
| 770 |
-
|
| 771 |
-
|
| 772 |
-
|
| 773 |
-
|
| 774 |
-
|
| 775 |
-
|
| 776 |
-
|
| 777 |
-
|
| 778 |
-
|
| 779 |
-
|
| 780 |
-
|
| 781 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 782 |
<p>Klik pada kategori untuk melihat contoh pertanyaan yang bisa diajukan</p>
|
| 783 |
-
|
| 784 |
-
|
| 785 |
-
|
| 786 |
-
|
| 787 |
-
|
| 788 |
-
|
| 789 |
-
<
|
| 790 |
-
|
| 791 |
-
|
| 792 |
-
|
| 793 |
-
|
| 794 |
-
|
| 795 |
-
|
| 796 |
-
|
| 797 |
-
|
| 798 |
-
|
| 799 |
-
|
| 800 |
-
|
| 801 |
-
|
| 802 |
-
|
| 803 |
-
|
| 804 |
-
|
| 805 |
-
|
| 806 |
-
|
| 807 |
-
|
| 808 |
-
|
| 809 |
-
|
| 810 |
-
|
| 811 |
-
|
| 812 |
-
|
| 813 |
-
|
| 814 |
-
|
| 815 |
-
|
| 816 |
-
|
| 817 |
-
|
| 818 |
-
|
| 819 |
-
|
| 820 |
-
|
| 821 |
-
|
| 822 |
-
|
| 823 |
-
|
| 824 |
-
|
| 825 |
-
|
| 826 |
-
|
| 827 |
-
|
| 828 |
-
</
|
| 829 |
-
|
|
|
|
|
|
|
|
|
|
| 830 |
</div>
|
| 831 |
-
|
| 832 |
-
|
| 833 |
-
|
| 834 |
-
|
| 835 |
-
|
| 836 |
-
|
| 837 |
-
|
| 838 |
-
|
| 839 |
-
|
| 840 |
-
|
| 841 |
-
|
| 842 |
-
|
| 843 |
-
|
| 844 |
-
|
| 845 |
-
|
| 846 |
-
|
| 847 |
-
|
| 848 |
-
|
| 849 |
-
|
| 850 |
-
|
| 851 |
-
|
| 852 |
-
|
| 853 |
-
|
| 854 |
-
|
| 855 |
-
|
| 856 |
-
|
| 857 |
-
|
| 858 |
-
|
| 859 |
-
|
| 860 |
-
|
| 861 |
-
|
| 862 |
-
|
| 863 |
-
|
| 864 |
-
|
| 865 |
-
|
| 866 |
-
|
| 867 |
-
|
| 868 |
-
|
| 869 |
-
|
| 870 |
-
|
| 871 |
-
|
| 872 |
-
|
| 873 |
-
|
| 874 |
-
|
| 875 |
-
|
| 876 |
-
|
| 877 |
-
|
| 878 |
-
|
| 879 |
-
|
| 880 |
-
|
| 881 |
-
|
| 882 |
-
|
| 883 |
-
|
| 884 |
-
|
| 885 |
-
|
| 886 |
-
|
| 887 |
-
|
| 888 |
-
|
| 889 |
-
|
| 890 |
-
|
| 891 |
-
|
| 892 |
-
|
| 893 |
-
|
| 894 |
-
|
| 895 |
-
|
| 896 |
-
|
| 897 |
-
|
| 898 |
-
|
| 899 |
-
|
| 900 |
-
|
| 901 |
-
|
| 902 |
-
|
| 903 |
-
|
| 904 |
-
|
| 905 |
-
|
| 906 |
-
|
| 907 |
-
|
| 908 |
-
|
| 909 |
-
|
| 910 |
-
|
| 911 |
-
|
| 912 |
-
|
| 913 |
-
|
| 914 |
-
|
| 915 |
-
|
| 916 |
-
|
| 917 |
-
|
| 918 |
-
|
| 919 |
-
|
| 920 |
-
|
| 921 |
-
|
| 922 |
-
|
| 923 |
-
|
| 924 |
-
|
| 925 |
-
|
| 926 |
-
|
| 927 |
-
|
| 928 |
-
|
| 929 |
-
|
| 930 |
-
|
| 931 |
-
|
| 932 |
-
|
| 933 |
-
|
| 934 |
-
|
| 935 |
-
|
| 936 |
-
|
| 937 |
-
|
| 938 |
-
|
| 939 |
-
|
| 940 |
-
|
| 941 |
-
|
| 942 |
-
|
| 943 |
-
|
| 944 |
-
|
| 945 |
-
|
| 946 |
-
|
| 947 |
-
|
| 948 |
-
|
| 949 |
-
|
| 950 |
-
|
| 951 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 952 |
π Starting Repository Chat Analysis
|
| 953 |
====================================
|
| 954 |
""")
|
| 955 |
|
| 956 |
-
|
| 957 |
-
|
|
|
|
| 16 |
# Load environment variables
|
| 17 |
dotenv.load_dotenv()
|
| 18 |
|
| 19 |
+
# Metadata# Metadata
|
| 20 |
+
CURRENT_TIME = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 21 |
+
CURRENT_USER = os.getenv("USER", "ErRickow")
|
| 22 |
|
| 23 |
# Default API Keys (fallback if user doesn't provide their own)
|
| 24 |
+
DEFAULT_XAI_KEY = os.getenv(
|
| 25 |
+
"XAI_API_KEY",
|
| 26 |
+
"xai-vfjhklL384Z4HKdItsZomqpFlXubTZJAFnISQUpV7dE8lRnWwYBVPSCxSTlu08wDbAcv720bx2dDiQ9x",
|
| 27 |
+
)
|
| 28 |
+
DEFAULT_GEMINI_KEY = os.getenv("GEMINI_API_KEY")
|
| 29 |
|
| 30 |
# API settings
|
| 31 |
+
OLLAMA_API = os.environ.get("OLLAMA_API", "http://localhost:11434")
|
| 32 |
+
XAI_BASE_URL = "https://api.x.ai/v1"
|
| 33 |
|
| 34 |
# Model lists
|
| 35 |
OLLAMA_MODELS = [
|
| 36 |
+
"llama2",
|
| 37 |
+
"codellama",
|
| 38 |
+
"mistral",
|
| 39 |
+
"neural-chat",
|
| 40 |
+
"starling-lm",
|
| 41 |
+
"dolphin-phi",
|
| 42 |
+
"phi",
|
| 43 |
+
"orca-mini",
|
| 44 |
]
|
| 45 |
|
| 46 |
XAI_MODELS = [
|
| 47 |
+
"grok-2-latest",
|
| 48 |
+
"grok-1",
|
| 49 |
]
|
| 50 |
|
| 51 |
GEMINI_MODELS = [
|
| 52 |
+
"gemini-1.5-mini",
|
| 53 |
+
"gemini-pro-vision",
|
| 54 |
]
|
| 55 |
|
| 56 |
# Help texts
|
|
|
|
| 115 |
|
| 116 |
|
| 117 |
class AIProvider:
|
| 118 |
+
OLLAMA = "ollama"
|
| 119 |
+
GEMINI = "gemini"
|
| 120 |
+
XAI = "xai"
|
| 121 |
|
| 122 |
|
| 123 |
class RepoAnalyzer:
|
| 124 |
+
def __init__(self):
|
| 125 |
+
self.current_repo = None
|
| 126 |
+
self.repo_content = {}
|
| 127 |
+
self.chat_history = []
|
| 128 |
+
|
| 129 |
+
async def stream_xai_response(
|
| 130 |
+
self, prompt: str, api_key: str = None, model: str = "grok-2-latest"
|
| 131 |
+
) -> AsyncGenerator[str, None]:
|
| 132 |
+
"""Stream response dari X.AI (Grok) API"""
|
| 133 |
+
try:
|
| 134 |
+
# Use default key if none provided
|
| 135 |
+
actual_key = api_key if api_key else DEFAULT_XAI_KEY
|
| 136 |
+
|
| 137 |
+
if not actual_key:
|
| 138 |
+
yield "β οΈ API Key X.AI diperlukan. Gunakan key Anda sendiri atau tunggu reset limit default key."
|
| 139 |
+
return
|
| 140 |
+
|
| 141 |
+
client = AsyncOpenAI(api_key=actual_key, base_url=XAI_BASE_URL)
|
| 142 |
+
|
| 143 |
+
# Prepare messages with repository context if available
|
| 144 |
+
messages = [
|
| 145 |
+
{
|
| 146 |
+
"role": "system",
|
| 147 |
+
"content": "Anda adalah asisten AI yang membantu menganalisis repository code. Berikan respons dalam Bahasa Indonesia.",
|
| 148 |
+
}
|
| 149 |
+
]
|
| 150 |
+
|
| 151 |
+
if self.current_repo:
|
| 152 |
+
context = f"Repository: {self.current_repo}\n\n"
|
| 153 |
+
repo_files = "\n".join(list(self.repo_content.keys()))
|
| 154 |
+
context += f"Files in repository:\n{repo_files}\n\n"
|
| 155 |
+
messages.append({"role": "system", "content": context})
|
| 156 |
+
|
| 157 |
+
messages.append({"role": "user", "content": prompt})
|
| 158 |
+
|
| 159 |
+
stream = await client.chat.completions.create(
|
| 160 |
+
model=model, messages=messages, stream=True
|
| 161 |
+
)
|
| 162 |
+
|
| 163 |
+
full_response = ""
|
| 164 |
+
async for chunk in stream:
|
| 165 |
+
if chunk.choices[0].delta.content:
|
| 166 |
+
content = chunk.choices[0].delta.content
|
| 167 |
+
full_response += content
|
| 168 |
+
yield content
|
| 169 |
+
|
| 170 |
+
self.chat_history.append({"role": "user", "content": prompt})
|
| 171 |
+
self.chat_history.append({"role": "assistant", "content": full_response})
|
| 172 |
+
|
| 173 |
+
except Exception as e:
|
| 174 |
+
error_msg = f"β οΈ Error dalam X.AI API: {str(e)}"
|
| 175 |
+
print(error_msg)
|
| 176 |
+
yield error_msg
|
| 177 |
+
|
| 178 |
+
async def stream_gemini_response(
|
| 179 |
+
self, prompt: str, api_key: str
|
| 180 |
+
) -> AsyncGenerator[str, None]:
|
| 181 |
+
"""Stream response dari Gemini API"""
|
| 182 |
+
try:
|
| 183 |
+
if not api_key:
|
| 184 |
+
yield "β οΈ API Key Gemini diperlukan. Klik icon bantuan (?) di samping input API Key untuk panduan mendapatkan key."
|
| 185 |
+
return
|
| 186 |
+
|
| 187 |
+
genai.configure(api_key=api_key)
|
| 188 |
+
model = genai.GenerativeModel("gemini-pro")
|
| 189 |
+
|
| 190 |
+
# Tambahkan konteks repository jika ada
|
| 191 |
+
if self.current_repo:
|
| 192 |
+
context = f"Repository: {self.current_repo}\n\n"
|
| 193 |
+
repo_files = "\n".join(list(self.repo_content.keys()))
|
| 194 |
+
context += f"Files in repository:\n{repo_files}\n\n"
|
| 195 |
+
prompt = context + prompt
|
| 196 |
+
|
| 197 |
+
response = model.generate_content(
|
| 198 |
+
prompt,
|
| 199 |
+
generation_config={"temperature": 0.7, "top_p": 0.8, "top_k": 40},
|
| 200 |
+
stream=True,
|
| 201 |
+
)
|
| 202 |
+
|
| 203 |
+
full_response = ""
|
| 204 |
+
async for chunk in response:
|
| 205 |
+
if chunk.text:
|
| 206 |
+
full_response += chunk.text
|
| 207 |
+
yield chunk.text
|
| 208 |
+
|
| 209 |
+
self.chat_history.append({"role": "user", "content": prompt})
|
| 210 |
+
self.chat_history.append({"role": "assistant", "content": full_response})
|
| 211 |
+
|
| 212 |
+
except Exception as e:
|
| 213 |
+
error_msg = f"β οΈ Error dalam Gemini API: {str(e)}\n\nPastikan API Key valid dan memiliki kuota yang cukup."
|
| 214 |
+
print(error_msg)
|
| 215 |
+
yield error_msg
|
| 216 |
+
|
| 217 |
+
def clone_repository(
|
| 218 |
+
self, repo_url: str, github_token: str, branch: str = None
|
| 219 |
+
) -> tuple[bool, str]:
|
| 220 |
+
"""Clone repository GitHub dengan autentikasi"""
|
| 221 |
+
if not repo_url:
|
| 222 |
+
return False, "β οΈ URL repository diperlukan"
|
| 223 |
+
|
| 224 |
+
repo_name = repo_url.split("/")[-1].replace(".git", "")
|
| 225 |
+
|
| 226 |
+
if os.path.exists(repo_name):
|
| 227 |
+
subprocess.run(["rm", "-rf", repo_name], check=True)
|
| 228 |
+
|
| 229 |
+
try:
|
| 230 |
+
owner_repo = "/".join(repo_url.split("/")[-2:])
|
| 231 |
+
|
| 232 |
+
# Cek apakah repository private
|
| 233 |
+
headers = {"Authorization": f"token {github_token}"} if github_token else {}
|
| 234 |
+
repo_check = requests.get(
|
| 235 |
+
f"https://api.github.com/repos/{owner_repo}", headers=headers
|
| 236 |
+
)
|
| 237 |
+
|
| 238 |
+
if repo_check.status_code == 404:
|
| 239 |
+
return False, "β οΈ Repository tidak ditemukan. Periksa URL repository."
|
| 240 |
+
elif repo_check.status_code == 401:
|
| 241 |
+
return (
|
| 242 |
+
False,
|
| 243 |
+
"β οΈ Token GitHub tidak valid. Klik icon bantuan (?) untuk panduan mendapatkan token.",
|
| 244 |
+
)
|
| 245 |
+
elif repo_check.status_code == 403 and repo_check.json().get(
|
| 246 |
+
"private", False
|
| 247 |
+
):
|
| 248 |
+
return (
|
| 249 |
+
False,
|
| 250 |
+
"β οΈ Ini adalah repository private. Token GitHub dengan akses 'repo' diperlukan.",
|
| 251 |
+
)
|
| 252 |
+
|
| 253 |
+
auth_url = (
|
| 254 |
+
f"https://{github_token}@github.com/{owner_repo}"
|
| 255 |
+
if github_token
|
| 256 |
+
else f"https://github.com/{owner_repo}"
|
| 257 |
+
)
|
| 258 |
+
|
| 259 |
+
cmd = ["git", "clone"]
|
| 260 |
+
if branch:
|
| 261 |
+
cmd.extend(["--branch", branch])
|
| 262 |
+
cmd.append(auth_url)
|
| 263 |
+
|
| 264 |
+
process = subprocess.run(
|
| 265 |
+
cmd,
|
| 266 |
+
capture_output=True,
|
| 267 |
+
text=True,
|
| 268 |
+
env=dict(os.environ, GIT_ASKPASS="echo", GIT_TERMINAL_PROMPT="0"),
|
| 269 |
+
)
|
| 270 |
+
|
| 271 |
+
if process.returncode == 0:
|
| 272 |
+
self.current_repo = repo_name
|
| 273 |
+
# Scan dan simpan konten repository
|
| 274 |
+
file_count = 0
|
| 275 |
+
for file_path in Path(repo_name).rglob("*"):
|
| 276 |
+
if file_path.is_file() and ".git" not in str(file_path):
|
| 277 |
+
success, content = self.read_file_safely(str(file_path))
|
| 278 |
+
if success:
|
| 279 |
+
self.repo_content[str(file_path)] = content
|
| 280 |
+
file_count += 1
|
| 281 |
+
|
| 282 |
+
return (
|
| 283 |
+
True,
|
| 284 |
+
f"β
Repository berhasil di-clone!\n\nNama: {repo_name}\nJumlah file: {file_count}\n\nAnda sekarang bisa mengajukan pertanyaan tentang repository ini.",
|
| 285 |
+
)
|
| 286 |
+
else:
|
| 287 |
+
return False, f"β οΈ Gagal clone repository:\n{process.stderr}"
|
| 288 |
+
|
| 289 |
+
except Exception as e:
|
| 290 |
+
return False, f"β οΈ Error: {str(e)}"
|
| 291 |
+
|
| 292 |
+
def read_file_safely(self, file_path: str) -> tuple[bool, str]:
|
| 293 |
+
"""Baca file dengan aman menggunakan berbagai encoding"""
|
| 294 |
+
encodings = ["utf-8", "latin-1", "cp1252"]
|
| 295 |
+
for encoding in encodings:
|
| 296 |
+
try:
|
| 297 |
+
with open(file_path, "r", encoding=encoding) as f:
|
| 298 |
+
content = f.read()
|
| 299 |
+
return True, content
|
| 300 |
+
except Exception as e:
|
| 301 |
+
continue
|
| 302 |
+
return False, "Tidak dapat membaca file dengan encoding yang didukung"
|
| 303 |
|
| 304 |
|
| 305 |
analyzer = RepoAnalyzer()
|
| 306 |
|
| 307 |
|
| 308 |
+
async def handle_chat(message, history, provider_choice, model_name, xai_key, gemini_key, selected_files, analyzer=analyzer):
|
| 309 |
+
"""Menangani interaksi chat dengan model AI"""
|
| 310 |
+
if not analyzer.current_repo:
|
| 311 |
+
new_message = {
|
| 312 |
+
"role": "assistant",
|
| 313 |
+
"content": "β οΈ Mohon clone repository terlebih dahulu sebelum mengajukan pertanyaan."
|
| 314 |
+
}
|
| 315 |
+
history = history or []
|
| 316 |
+
history.append({"role": "user", "content": message})
|
| 317 |
+
history.append(new_message)
|
| 318 |
+
yield history
|
| 319 |
+
return
|
| 320 |
+
|
| 321 |
+
history = history or []
|
| 322 |
+
|
| 323 |
+
# Format user message
|
| 324 |
+
user_message = f'<div class="user-message">{message}</div>'
|
| 325 |
+
history.append({"role": "user", "content": user_message})
|
| 326 |
+
|
| 327 |
+
# Initialize assistant message
|
| 328 |
+
assistant_message = '<div class="assistant-message">'
|
| 329 |
+
history.append({"role": "assistant", "content": assistant_message})
|
| 330 |
+
|
| 331 |
+
try:
|
| 332 |
+
# Add context about selected files to the prompt
|
| 333 |
+
file_context = ""
|
| 334 |
+
if selected_files:
|
| 335 |
+
file_context = "\n\nFile yang dipilih:\n"
|
| 336 |
+
for file in selected_files:
|
| 337 |
+
content = analyzer.repo_content.get(file, "")
|
| 338 |
+
if content:
|
| 339 |
+
file_context += f"\n{file}:\n```\n{content}\n```\n"
|
| 340 |
+
|
| 341 |
+
enhanced_message = f"{message}\n{file_context}"
|
| 342 |
+
|
| 343 |
+
full_response = ""
|
| 344 |
+
if provider_choice == AIProvider.XAI:
|
| 345 |
+
async for chunk in analyzer.stream_xai_response(enhanced_message, xai_key, model_name):
|
| 346 |
+
full_response += chunk
|
| 347 |
+
# Format the response with code blocks
|
| 348 |
+
formatted_response = format_response(full_response)
|
| 349 |
+
history[-1]["content"] = f'<div class="assistant-message">{formatted_response}</div>'
|
| 350 |
+
await asyncio.sleep(0.05)
|
| 351 |
+
yield history
|
| 352 |
+
|
| 353 |
+
elif provider_choice == AIProvider.GEMINI:
|
| 354 |
+
async for chunk in analyzer.stream_gemini_response(enhanced_message, gemini_key or DEFAULT_GEMINI_KEY):
|
| 355 |
+
full_response += chunk
|
| 356 |
+
formatted_response = format_response(full_response)
|
| 357 |
+
history[-1]["content"] = f'<div class="assistant-message">{formatted_response}</div>'
|
| 358 |
+
await asyncio.sleep(0.05)
|
| 359 |
+
yield history
|
| 360 |
+
|
| 361 |
+
else: # OLLAMA
|
| 362 |
+
response = analyze_with_ollama(model_name, enhanced_message)
|
| 363 |
+
words = response.split()
|
| 364 |
+
for i in range(len(words)):
|
| 365 |
+
full_response = " ".join(words[:i + 1])
|
| 366 |
+
formatted_response = format_response(full_response)
|
| 367 |
+
history[-1]["content"] = f'<div class="assistant-message">{formatted_response}</div>'
|
| 368 |
+
await asyncio.sleep(0.05)
|
| 369 |
+
yield history
|
| 370 |
+
|
| 371 |
+
except Exception as e:
|
| 372 |
+
error_msg = f"β οΈ Error: {str(e)}"
|
| 373 |
+
history[-1]["content"] = f'<div class="assistant-message">{error_msg}</div>'
|
| 374 |
+
yield history
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 375 |
|
| 376 |
def format_response(text):
|
| 377 |
+
"""Format response text with code blocks"""
|
| 378 |
+
import re
|
| 379 |
+
import uuid
|
| 380 |
+
|
| 381 |
+
# Replace code blocks with styled versions
|
| 382 |
+
def replace_code_block(match):
|
| 383 |
+
language = match.group(1) or ''
|
| 384 |
+
code = match.group(2)
|
| 385 |
+
block_id = f'code-block-{uuid.uuid4().hex[:8]}'
|
| 386 |
+
|
| 387 |
+
return f'''
|
| 388 |
<div class="code-block">
|
| 389 |
<div class="code-header">
|
| 390 |
<span class="code-title">{language}</span>
|
|
|
|
| 394 |
</div>
|
| 395 |
'''
|
| 396 |
|
| 397 |
+
# Replace ```language\ncode``` blocks
|
| 398 |
+
text = re.sub(r'```(\w+)?\n(.*?)```', replace_code_block, text, flags=re.DOTALL)
|
| 399 |
+
|
| 400 |
+
# Replace inline code
|
| 401 |
+
text = re.sub(r'`([^`]+)`', r'<code>\1</code>', text)
|
| 402 |
+
|
| 403 |
+
return text
|
| 404 |
|
| 405 |
|
| 406 |
def create_ui():
|
| 407 |
+
global analyzer
|
| 408 |
+
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 409 |
|
| 410 |
+
with gr.Blocks(title="Open Repo AI", theme=gr.themes.Soft()) as app:
|
| 411 |
+
# CSS Styling
|
| 412 |
+
gr.Markdown("""
|
| 413 |
<style>
|
| 414 |
/* General Styles */
|
| 415 |
.container { max-width: 100% !important; padding: 1rem; }
|
|
|
|
| 590 |
</script>
|
| 591 |
""")
|
| 592 |
|
| 593 |
+
# Header
|
| 594 |
+
with gr.Row(elem_classes="container"):
|
| 595 |
+
gr.Markdown(f"""
|
| 596 |
# AI Github Repository Chat
|
| 597 |
|
| 598 |
<div class="header-info">
|
| 599 |
<span class="timestamp">Current Date and Time (UTC - YYYY-MM-DD HH:MM:SS formatted): {current_time}</span>
|
|
|
|
| 600 |
</div>
|
| 601 |
""")
|
| 602 |
|
| 603 |
+
# Main Tabs Container
|
| 604 |
+
with gr.Tabs() as tabs:
|
| 605 |
+
# Configuration Tab
|
| 606 |
+
with gr.Tab("π οΈ Konfigurasi"):
|
| 607 |
+
provider = gr.Radio(
|
| 608 |
+
choices=[AIProvider.XAI, AIProvider.GEMINI, AIProvider.OLLAMA],
|
| 609 |
+
label="Penyedia AI",
|
| 610 |
+
value=AIProvider.XAI
|
| 611 |
+
)
|
| 612 |
+
|
| 613 |
+
with gr.Group() as api_settings:
|
| 614 |
+
# X.AI API Key section
|
| 615 |
+
with gr.Row():
|
| 616 |
+
with gr.Column(scale=3):
|
| 617 |
+
xai_key = gr.Textbox(
|
| 618 |
+
label="X.AI (Grok) API Key",
|
| 619 |
+
type="password",
|
| 620 |
+
placeholder="Opsional - Klik (?) untuk info",
|
| 621 |
+
show_label=True
|
| 622 |
+
)
|
| 623 |
+
with gr.Column(scale=1):
|
| 624 |
+
gr.Markdown("""
|
|
|
|
| 625 |
<details>
|
| 626 |
<summary>β Cara Mendapatkan X.AI API Key</summary>
|
| 627 |
<div class="help-content">
|
|
|
|
| 640 |
</details>
|
| 641 |
""")
|
| 642 |
|
| 643 |
+
# Gemini API Key section
|
| 644 |
+
with gr.Row():
|
| 645 |
+
with gr.Column(scale=3):
|
| 646 |
+
gemini_key = gr.Textbox(
|
| 647 |
+
label="Gemini API Key",
|
| 648 |
+
type="password",
|
| 649 |
+
placeholder="Opsional - Klik (?) untuk info",
|
| 650 |
+
show_label=True
|
| 651 |
+
)
|
| 652 |
+
with gr.Column(scale=1):
|
| 653 |
+
gr.Markdown("""
|
| 654 |
<details>
|
| 655 |
<summary>β Cara Mendapatkan Gemini API Key</summary>
|
| 656 |
<div class="help-content">
|
|
|
|
| 670 |
</details>
|
| 671 |
""")
|
| 672 |
|
| 673 |
+
with gr.Row():
|
| 674 |
+
model_dropdown = gr.Dropdown(
|
| 675 |
+
label="Model AI",
|
| 676 |
+
choices=XAI_MODELS,
|
| 677 |
+
value="grok-2-latest",
|
| 678 |
+
interactive=True
|
| 679 |
+
)
|
| 680 |
+
|
| 681 |
+
# Repository Analysis Tab
|
| 682 |
+
with gr.Tab("π Analisis Repository"):
|
| 683 |
+
with gr.Group():
|
| 684 |
+
# Repository URL input
|
| 685 |
+
with gr.Row():
|
| 686 |
+
repo_url = gr.Textbox(
|
| 687 |
+
label="URL Repository GitHub",
|
| 688 |
+
placeholder="https://github.com/username/repository",
|
| 689 |
+
elem_classes="mobile-full"
|
| 690 |
+
)
|
| 691 |
+
|
| 692 |
+
# GitHub Token section
|
| 693 |
+
with gr.Row():
|
| 694 |
+
with gr.Column(scale=2):
|
| 695 |
+
github_token = gr.Textbox(
|
| 696 |
+
label="Token GitHub",
|
| 697 |
+
type="password",
|
| 698 |
+
placeholder="Klik (?) untuk panduan",
|
| 699 |
+
elem_classes="mobile-full"
|
| 700 |
+
)
|
| 701 |
+
gr.Markdown("""
|
| 702 |
<details>
|
| 703 |
<summary>β Cara Mendapatkan GitHub Token</summary>
|
| 704 |
<div class="help-content">
|
|
|
|
| 718 |
</div>
|
| 719 |
</details>
|
| 720 |
""")
|
| 721 |
+
with gr.Column(scale=1):
|
| 722 |
+
branch = gr.Textbox(
|
| 723 |
+
label="Branch (opsional)",
|
| 724 |
+
placeholder="main",
|
| 725 |
+
elem_classes="mobile-full"
|
| 726 |
+
)
|
| 727 |
+
|
| 728 |
+
# Clone Button and Status
|
| 729 |
+
clone_button = gr.Button(
|
| 730 |
+
"π Clone Repository",
|
| 731 |
+
variant="primary",
|
| 732 |
+
elem_classes="mobile-full"
|
| 733 |
+
)
|
| 734 |
+
|
| 735 |
+
clone_status = gr.Markdown(
|
| 736 |
+
value="",
|
| 737 |
+
label="Status Repository",
|
| 738 |
+
elem_classes="mobile-full"
|
| 739 |
+
)
|
| 740 |
+
|
| 741 |
+
# File Selection
|
| 742 |
+
with gr.Group():
|
| 743 |
+
gr.Markdown("### π File yang Dipilih")
|
| 744 |
+
with gr.Row():
|
| 745 |
+
file_selector = gr.Dropdown(
|
| 746 |
+
label="Pilih File dari Repository",
|
| 747 |
+
choices=[],
|
| 748 |
+
multiselect=True,
|
| 749 |
+
value=[],
|
| 750 |
+
allow_custom_value=True,
|
| 751 |
+
max_choices=None,
|
| 752 |
+
elem_classes="mobile-full"
|
| 753 |
+
)
|
| 754 |
+
|
| 755 |
+
file_list = gr.HTML(
|
| 756 |
+
value="<div class='file-list'>Belum ada file yang dipilih</div>",
|
| 757 |
+
label="Daftar File Terpilih"
|
| 758 |
+
)
|
| 759 |
+
|
| 760 |
+
# Examples Tab
|
| 761 |
+
with gr.Tab("π‘ Examples"):
|
| 762 |
+
gr.Markdown("""
|
| 763 |
+
<style>
|
| 764 |
+
/* Styling untuk collapse */
|
| 765 |
+
.examples-container {
|
| 766 |
+
padding: 20px;
|
| 767 |
+
background: #fff;
|
| 768 |
+
border-radius: 8px;
|
| 769 |
+
}
|
| 770 |
+
|
| 771 |
+
.category-collapse {
|
| 772 |
+
margin-bottom: 15px;
|
| 773 |
+
border: 1px solid #e9ecef;
|
| 774 |
+
border-radius: 8px;
|
| 775 |
+
overflow: hidden;
|
| 776 |
+
}
|
| 777 |
+
|
| 778 |
+
.category-header {
|
| 779 |
+
padding: 15px;
|
| 780 |
+
background: #f8f9fa;
|
| 781 |
+
cursor: pointer;
|
| 782 |
+
user-select: none;
|
| 783 |
+
display: flex;
|
| 784 |
+
align-items: center;
|
| 785 |
+
justify-content: space-between;
|
| 786 |
+
transition: background-color 0.2s;
|
| 787 |
+
}
|
| 788 |
+
|
| 789 |
+
.category-header:hover {
|
| 790 |
+
background: #e9ecef;
|
| 791 |
+
}
|
| 792 |
+
|
| 793 |
+
.category-content {
|
| 794 |
+
padding: 15px;
|
| 795 |
+
display: none;
|
| 796 |
+
border-top: 1px solid #e9ecef;
|
| 797 |
+
}
|
| 798 |
+
|
| 799 |
+
.category-collapse.active .category-content {
|
| 800 |
+
display: block;
|
| 801 |
+
animation: slideDown 0.3s ease-out;
|
| 802 |
+
}
|
| 803 |
+
|
| 804 |
+
.category-header::after {
|
| 805 |
+
content: 'βΌ';
|
| 806 |
+
font-size: 12px;
|
| 807 |
+
transition: transform 0.3s;
|
| 808 |
+
}
|
| 809 |
+
|
| 810 |
+
.category-collapse.active .category-header::after {
|
| 811 |
+
transform: rotate(180deg);
|
| 812 |
+
}
|
| 813 |
+
|
| 814 |
+
.example-button {
|
| 815 |
+
display: block;
|
| 816 |
+
width: 100%;
|
| 817 |
+
padding: 10px;
|
| 818 |
+
margin: 5px 0;
|
| 819 |
+
text-align: left;
|
| 820 |
+
background: #fff;
|
| 821 |
+
border: 1px solid #e9ecef;
|
| 822 |
+
border-radius: 4px;
|
| 823 |
+
cursor: pointer;
|
| 824 |
+
transition: all 0.2s;
|
| 825 |
+
}
|
| 826 |
+
|
| 827 |
+
.example-button:hover {
|
| 828 |
+
background: #f8f9fa;
|
| 829 |
+
border-color: #dee2e6;
|
| 830 |
+
}
|
| 831 |
+
|
| 832 |
+
@keyframes slideDown {
|
| 833 |
+
from {
|
| 834 |
+
opacity: 0;
|
| 835 |
+
transform: translateY(-10px);
|
| 836 |
+
}
|
| 837 |
+
to {
|
| 838 |
+
opacity: 1;
|
| 839 |
+
transform: translateY(0);
|
| 840 |
+
}
|
| 841 |
+
}
|
| 842 |
+
</style>
|
| 843 |
+
|
| 844 |
+
<div class="examples-container">
|
| 845 |
+
<h3>π― Contoh Pertanyaan</h3>
|
| 846 |
<p>Klik pada kategori untuk melihat contoh pertanyaan yang bisa diajukan</p>
|
| 847 |
+
|
| 848 |
+
<div class="category-collapse">
|
| 849 |
+
<div class="category-header">
|
| 850 |
+
π Analisis Kode
|
| 851 |
+
</div>
|
| 852 |
+
<div class="category-content">
|
| 853 |
+
<button class="example-button" onclick="setQuestion('Jelaskan logika dan alur dari kode ini')">
|
| 854 |
+
Jelaskan logika dari kode
|
| 855 |
+
</button>
|
| 856 |
+
<button class="example-button" onclick="setQuestion('Bagaimana cara mengoptimalkan performa kode ini?')">
|
| 857 |
+
Optimasi performa
|
| 858 |
+
</button>
|
| 859 |
+
<button class="example-button" onclick="setQuestion('Apakah ada potential bugs yang perlu diperbaiki?')">
|
| 860 |
+
Cek potential bugs
|
| 861 |
+
</button>
|
| 862 |
+
</div>
|
| 863 |
+
</div>
|
| 864 |
+
|
| 865 |
+
<div class="category-collapse">
|
| 866 |
+
<div class="category-header">
|
| 867 |
+
π Best Practices
|
| 868 |
+
</div>
|
| 869 |
+
<div class="category-content">
|
| 870 |
+
<button class="example-button" onclick="setQuestion('Apa saja best practices yang bisa diterapkan di kode ini?')">
|
| 871 |
+
Saran best practices
|
| 872 |
+
</button>
|
| 873 |
+
<button class="example-button" onclick="setQuestion('Bagaimana cara meningkatkan maintainability kode ini?')">
|
| 874 |
+
Improve maintainability
|
| 875 |
+
</button>
|
| 876 |
+
<button class="example-button" onclick="setQuestion('Berikan saran untuk meningkatkan code quality')">
|
| 877 |
+
Improve code quality
|
| 878 |
+
</button>
|
| 879 |
+
</div>
|
| 880 |
+
</div>
|
| 881 |
+
|
| 882 |
+
<div class="category-collapse">
|
| 883 |
+
<div class="category-header">
|
| 884 |
+
π§ͺ Testing
|
| 885 |
+
</div>
|
| 886 |
+
<div class="category-content">
|
| 887 |
+
<button class="example-button" onclick="setQuestion('Buatkan unit test untuk fungsi ini')">
|
| 888 |
+
Generate unit tests
|
| 889 |
+
</button>
|
| 890 |
+
<button class="example-button" onclick="setQuestion('Bagaimana cara test error handling yang baik?')">
|
| 891 |
+
Test error handling
|
| 892 |
+
</button>
|
| 893 |
+
<button class="example-button" onclick="setQuestion('Berikan saran untuk meningkatkan test coverage')">
|
| 894 |
+
Improve test coverage
|
| 895 |
+
</button>
|
| 896 |
+
</div>
|
| 897 |
</div>
|
| 898 |
+
|
| 899 |
+
<div class="category-collapse">
|
| 900 |
+
<div class="category-header">
|
| 901 |
+
π Dokumentasi
|
| 902 |
+
</div>
|
| 903 |
+
<div class="category-content">
|
| 904 |
+
<button class="example-button" onclick="setQuestion('Buatkan dokumentasi untuk fungsi-fungsi utama')">
|
| 905 |
+
Generate documentation
|
| 906 |
+
</button>
|
| 907 |
+
<button class="example-button" onclick="setQuestion('Buat README.md untuk project ini')">
|
| 908 |
+
Create README.md
|
| 909 |
+
</button>
|
| 910 |
+
<button class="example-button" onclick="setQuestion('Jelaskan cara penggunaan library/framework')">
|
| 911 |
+
Usage guide
|
| 912 |
+
</button>
|
| 913 |
+
</div>
|
| 914 |
+
</div>
|
| 915 |
+
</div>
|
| 916 |
+
|
| 917 |
+
<script>
|
| 918 |
+
document.addEventListener('DOMContentLoaded', function() {
|
| 919 |
+
// Handle collapse functionality
|
| 920 |
+
const categories = document.querySelectorAll('.category-collapse');
|
| 921 |
+
|
| 922 |
+
categories.forEach(category => {
|
| 923 |
+
const header = category.querySelector('.category-header');
|
| 924 |
+
|
| 925 |
+
header.addEventListener('click', () => {
|
| 926 |
+
const isActive = category.classList.contains('active');
|
| 927 |
+
|
| 928 |
+
// Close all categories
|
| 929 |
+
categories.forEach(c => c.classList.remove('active'));
|
| 930 |
+
|
| 931 |
+
// If clicked category wasn't active, open it
|
| 932 |
+
if (!isActive) {
|
| 933 |
+
category.classList.add('active');
|
| 934 |
+
}
|
| 935 |
+
});
|
| 936 |
+
});
|
| 937 |
+
});
|
| 938 |
+
|
| 939 |
+
// Function to set question in chat input
|
| 940 |
+
function setQuestion(text) {
|
| 941 |
+
const chatInput = document.querySelector('textarea[data-testid="textbox"]');
|
| 942 |
+
if (chatInput) {
|
| 943 |
+
chatInput.value = text;
|
| 944 |
+
chatInput.focus();
|
| 945 |
+
}
|
| 946 |
+
}
|
| 947 |
+
</script>
|
| 948 |
+
""")
|
| 949 |
+
|
| 950 |
+
# Chat Interface (outside tabs)
|
| 951 |
+
# with gr.Group():
|
| 952 |
+
# chat_history = gr.Chatbot(
|
| 953 |
+
# label="π Riwayat Chat",
|
| 954 |
+
# height=500,
|
| 955 |
+
# show_label=True,
|
| 956 |
+
# type="messages",
|
| 957 |
+
# elem_classes="mobile-full"
|
| 958 |
+
# )
|
| 959 |
+
|
| 960 |
+
# with gr.Row():
|
| 961 |
+
# chat_input = gr.Textbox(
|
| 962 |
+
# label="π Tanyakan tentang Repository",
|
| 963 |
+
# placeholder="Ketik pertanyaan Anda di sini...",
|
| 964 |
+
# lines=3,
|
| 965 |
+
# elem_classes="mobile-full"
|
| 966 |
+
# )
|
| 967 |
+
# send_button = gr.Button("π€ Kirim", variant="primary")
|
| 968 |
+
# clear_button = gr.Button("π§Ή Bersihkan", variant="secondary")
|
| 969 |
+
|
| 970 |
+
# Event Handlers
|
| 971 |
+
def handle_clone(repo_url, github_token, branch):
|
| 972 |
+
if not repo_url:
|
| 973 |
+
return (
|
| 974 |
+
"β οΈ URL repository diperlukan!",
|
| 975 |
+
gr.Dropdown(choices=[]),
|
| 976 |
+
"<div class='file-list'>Belum ada file yang dipilih</div>"
|
| 977 |
+
)
|
| 978 |
+
|
| 979 |
+
success, message = analyzer.clone_repository(repo_url, github_token, branch)
|
| 980 |
+
|
| 981 |
+
if success:
|
| 982 |
+
files = sorted(list(analyzer.repo_content.keys()))
|
| 983 |
+
return (
|
| 984 |
+
message,
|
| 985 |
+
gr.Dropdown(choices=files, value=[]),
|
| 986 |
+
"<div class='file-list'>Belum ada file yang dipilih</div>"
|
| 987 |
+
)
|
| 988 |
+
|
| 989 |
+
return (
|
| 990 |
+
message,
|
| 991 |
+
gr.Dropdown(choices=[]),
|
| 992 |
+
"<div class='file-list'>Belum ada file yang dipilih</div>"
|
| 993 |
+
)
|
| 994 |
+
|
| 995 |
+
def update_file_list(selected):
|
| 996 |
+
if not selected:
|
| 997 |
+
return "<div class='file-list'>Belum ada file yang dipilih</div>"
|
| 998 |
+
|
| 999 |
+
html = "<div class='file-list'>"
|
| 1000 |
+
for file in selected:
|
| 1001 |
+
html += f"<div class='file-item'><span>{file}</span></div>"
|
| 1002 |
+
html += "</div>"
|
| 1003 |
+
return html
|
| 1004 |
+
|
| 1005 |
+
def clear_chat_history():
|
| 1006 |
+
return []
|
| 1007 |
+
|
| 1008 |
+
def update_model_list(provider_choice):
|
| 1009 |
+
if provider_choice == AIProvider.XAI:
|
| 1010 |
+
return gr.Dropdown(choices=XAI_MODELS, value="grok-2-latest")
|
| 1011 |
+
elif provider_choice == AIProvider.GEMINI:
|
| 1012 |
+
return gr.Dropdown(choices=GEMINI_MODELS, value="gemini-1.5-mini")
|
| 1013 |
+
else: # OLLAMA
|
| 1014 |
+
return gr.Dropdown(choices=OLLAMA_MODELS, value="llama2")
|
| 1015 |
+
|
| 1016 |
+
# Connect Events
|
| 1017 |
+
provider.change(
|
| 1018 |
+
fn=update_model_list,
|
| 1019 |
+
inputs=[provider],
|
| 1020 |
+
outputs=[model_dropdown]
|
| 1021 |
+
)
|
| 1022 |
+
|
| 1023 |
+
clone_button.click(
|
| 1024 |
+
fn=handle_clone,
|
| 1025 |
+
inputs=[repo_url, github_token, branch],
|
| 1026 |
+
outputs=[clone_status, file_selector, file_list]
|
| 1027 |
+
)
|
| 1028 |
+
|
| 1029 |
+
file_selector.change(
|
| 1030 |
+
fn=update_file_list,
|
| 1031 |
+
inputs=[file_selector],
|
| 1032 |
+
outputs=[file_list]
|
| 1033 |
+
)
|
| 1034 |
+
|
| 1035 |
+
# Chat events
|
| 1036 |
+
clear_button.click(
|
| 1037 |
+
fn=clear_chat_history,
|
| 1038 |
+
outputs=[chat_history]
|
| 1039 |
+
)
|
| 1040 |
+
|
| 1041 |
+
send_button.click(
|
| 1042 |
+
fn=handle_chat,
|
| 1043 |
+
inputs=[
|
| 1044 |
+
chat_input,
|
| 1045 |
+
chat_history,
|
| 1046 |
+
provider,
|
| 1047 |
+
model_dropdown,
|
| 1048 |
+
xai_key,
|
| 1049 |
+
gemini_key,
|
| 1050 |
+
file_selector
|
| 1051 |
+
],
|
| 1052 |
+
outputs=chat_history,
|
| 1053 |
+
show_progress=True
|
| 1054 |
+
).then(
|
| 1055 |
+
fn=lambda: gr.update(value=""),
|
| 1056 |
+
outputs=chat_input
|
| 1057 |
+
)
|
| 1058 |
+
|
| 1059 |
+
chat_input.submit(
|
| 1060 |
+
fn=handle_chat,
|
| 1061 |
+
inputs=[
|
| 1062 |
+
chat_input,
|
| 1063 |
+
chat_history,
|
| 1064 |
+
provider,
|
| 1065 |
+
model_dropdown,
|
| 1066 |
+
xai_key,
|
| 1067 |
+
gemini_key,
|
| 1068 |
+
file_selector
|
| 1069 |
+
],
|
| 1070 |
+
outputs=chat_history,
|
| 1071 |
+
show_progress=True
|
| 1072 |
+
).then(
|
| 1073 |
+
fn=lambda: gr.update(value=""),
|
| 1074 |
+
outputs=chat_input
|
| 1075 |
+
)
|
| 1076 |
+
|
| 1077 |
+
return app
|
| 1078 |
+
|
| 1079 |
+
|
| 1080 |
+
if __name__ == "__main__":
|
| 1081 |
+
print("""
|
| 1082 |
π Starting Repository Chat Analysis
|
| 1083 |
====================================
|
| 1084 |
""")
|
| 1085 |
|
| 1086 |
+
app = create_ui()
|
| 1087 |
+
app.launch(share=True, server_name="0.0.0.0", server_port=7860, debug=True)
|