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
 
 
 
 
 
 
 
ec6c0cf
 
 
 
038ccc8
 
 
 
 
 
 
 
ec6c0cf
 
 
 
f86a66f
 
ec6c0cf
 
038ccc8
 
 
 
 
 
 
 
ec6c0cf
 
 
 
 
f86a66f
 
 
 
 
ec6c0cf
 
 
f86a66f
 
 
 
1
2
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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
303
304
305
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
import gradio as gr
import os
from pathlib import Path
import subprocess
import requests
import json
from datetime import datetime
import textwrap
import google.generativeai as genai
import asyncio
from typing import Generator, AsyncGenerator
from openai import AsyncOpenAI
import dotenv

# Load environment variables
dotenv.load_dotenv()

# Metadata
CURRENT_TIME = "2025-05-23 12:57:22"
CURRENT_USER = "ErRickow"

# Default API Keys (fallback if user doesn't provide their own)
DEFAULT_XAI_KEY = os.getenv("XAI_API_KEY", "xai-vfjhklL384Z4HKdItsZomqpFlXubTZJAFnISQUpV7dE8lRnWwYBVPSCxSTlu08wDbAcv720bx2dDiQ9x")
DEFAULT_GEMINI_KEY = os.getenv("GEMINI_API_KEY")

# API settings
OLLAMA_API = os.environ.get("OLLAMA_API", "http://localhost:11434")
XAI_BASE_URL = "https://api.x.ai/v1"

# Model lists
OLLAMA_MODELS = [
    "llama2",
    "codellama",
    "mistral",
    "neural-chat",
    "starling-lm",
    "dolphin-phi",
    "phi",
    "orca-mini"
]

XAI_MODELS = [
    "grok-2-latest",
    "grok-1",
]

GEMINI_MODELS = [
    "gemini-1.5-mini",
    "gemini-pro-vision",
]

# Help texts
XAI_API_HELP = """
### Cara Mendapatkan X.AI (Grok) API Key:

1. Kunjungi [X.AI Developer Portal](https://x.ai)
2. Daftar/Login ke akun Anda
3. Buat API Key baru
4. Salin API Key

Note:
- Jika tidak diisi, akan menggunakan API key default
- Masukkan API key Anda sendiri jika default mencapai limit
"""

class AIProvider:
    OLLAMA = "ollama"
    GEMINI = "gemini"
    XAI = "xai"

class RepoAnalyzer:
    def __init__(self):
        self.current_repo = None
        self.repo_content = {}
        self.chat_history = []

    async def stream_xai_response(self, prompt: str, api_key: str = None, model: str = "grok-2-latest") -> AsyncGenerator[str, None]:
        """Stream response dari X.AI (Grok) API"""
        try:
            # Use default key if none provided
            actual_key = api_key if api_key else DEFAULT_XAI_KEY
            
            if not actual_key:
                yield "⚠️ API Key X.AI diperlukan. Gunakan key Anda sendiri atau tunggu reset limit default key."
                return

            client = AsyncOpenAI(
                api_key=actual_key,
                base_url=XAI_BASE_URL
            )

            # Prepare messages with repository context if available
            messages = [
                {"role": "system", "content": "Anda adalah asisten AI yang membantu menganalisis repository code. Berikan respons dalam Bahasa Indonesia."}
            ]
            
            if self.current_repo:
                context = f"Repository: {self.current_repo}\n\n"
                repo_files = "\n".join(list(self.repo_content.keys()))
                context += f"Files in repository:\n{repo_files}\n\n"
                messages.append({"role": "system", "content": context})

            messages.append({"role": "user", "content": prompt})

            stream = await client.chat.completions.create(
                model=model,
                messages=messages,
                stream=True
            )

            full_response = ""
            async for chunk in stream:
                if chunk.choices[0].delta.content:
                    content = chunk.choices[0].delta.content
                    full_response += content
                    yield content

            self.chat_history.append({"role": "user", "content": prompt})
            self.chat_history.append({"role": "assistant", "content": full_response})

        except Exception as e:
            error_msg = f"⚠️ Error dalam X.AI API: {str(e)}"
            print(error_msg)
            yield error_msg

    async def stream_gemini_response(self, prompt: str, api_key: str) -> AsyncGenerator[str, None]:
        """Stream response dari Gemini API"""
        try:
            if not api_key:
                yield "⚠️ API Key Gemini diperlukan. Klik icon bantuan (?) di samping input API Key untuk panduan mendapatkan key."
                return

            genai.configure(api_key=api_key)
            model = genai.GenerativeModel('gemini-pro')
            
            # Tambahkan konteks repository jika ada
            if self.current_repo:
                context = f"Repository: {self.current_repo}\n\n"
                repo_files = "\n".join(list(self.repo_content.keys()))
                context += f"Files in repository:\n{repo_files}\n\n"
                prompt = context + prompt
            
            response = model.generate_content(
                prompt,
                generation_config={
                    "temperature": 0.7,
                    "top_p": 0.8,
                    "top_k": 40
                },
                stream=True
            )
            
            full_response = ""
            async for chunk in response:
                if chunk.text:
                    full_response += chunk.text
                    yield chunk.text
            
            self.chat_history.append({"role": "user", "content": prompt})
            self.chat_history.append({"role": "assistant", "content": full_response})
            
        except Exception as e:
            error_msg = f"⚠️ Error dalam Gemini API: {str(e)}\n\nPastikan API Key valid dan memiliki kuota yang cukup."
            print(error_msg)
            yield error_msg

    def clone_repository(self, repo_url: str, github_token: str, branch: str = None) -> tuple[bool, str]:
        """Clone repository GitHub dengan autentikasi"""
        if not repo_url:
            return False, "⚠️ URL repository diperlukan"

        repo_name = repo_url.split('/')[-1].replace('.git', '')
        
        if os.path.exists(repo_name):
            subprocess.run(['rm', '-rf', repo_name], check=True)

        try:
            owner_repo = '/'.join(repo_url.split('/')[-2:])
            
            # Cek apakah repository private
            headers = {'Authorization': f'token {github_token}'} if github_token else {}
            repo_check = requests.get(f"https://api.github.com/repos/{owner_repo}", headers=headers)
            
            if repo_check.status_code == 404:
                return False, "⚠️ Repository tidak ditemukan. Periksa URL repository."
            elif repo_check.status_code == 401:
                return False, "⚠️ Token GitHub tidak valid. Klik icon bantuan (?) untuk panduan mendapatkan token."
            elif repo_check.status_code == 403 and repo_check.json().get('private', False):
                return False, "⚠️ Ini adalah repository private. Token GitHub dengan akses 'repo' diperlukan."

            auth_url = f"https://{github_token}@github.com/{owner_repo}" if github_token else f"https://github.com/{owner_repo}"

            cmd = ['git', 'clone']
            if branch:
                cmd.extend(['--branch', branch])
            cmd.append(auth_url)

            process = subprocess.run(
                cmd,
                capture_output=True,
                text=True,
                env=dict(os.environ, GIT_ASKPASS='echo', GIT_TERMINAL_PROMPT='0')
            )

            if process.returncode == 0:
                self.current_repo = repo_name
                # Scan dan simpan konten repository
                file_count = 0
                for file_path in Path(repo_name).rglob('*'):
                    if file_path.is_file() and '.git' not in str(file_path):
                        success, content = self.read_file_safely(str(file_path))
                        if success:
                            self.repo_content[str(file_path)] = content
                            file_count += 1
                
                return True, f"βœ… Repository berhasil di-clone!\n\nNama: {repo_name}\nJumlah file: {file_count}\n\nAnda sekarang bisa mengajukan pertanyaan tentang repository ini."
            else:
                return False, f"⚠️ Gagal clone repository:\n{process.stderr}"
                
        except Exception as e:
            return False, f"⚠️ Error: {str(e)}"

    def read_file_safely(self, file_path: str) -> tuple[bool, str]:
        """Baca file dengan aman menggunakan berbagai encoding"""
        encodings = ['utf-8', 'latin-1', 'cp1252']
        for encoding in encodings:
            try:
                with open(file_path, 'r', encoding=encoding) as f:
                    content = f.read()
                    return True, content
            except Exception as e:
                continue
        return False, "Tidak dapat membaca file dengan encoding yang didukung"

def create_ui():
    analyzer = RepoAnalyzer()
    
    with gr.Blocks(title="Repository Chat Analysis") as app:
        gr.Markdown(f"""
        # πŸ€– Repository Chat Analysis
        
        πŸ“… Waktu: {CURRENT_TIME}
        πŸ‘€ Pengguna: {CURRENT_USER}
        """)

        with gr.Tab("πŸ› οΈ Konfigurasi"):
            provider = gr.Radio(
                choices=[AIProvider.XAI, AIProvider.GEMINI, AIProvider.OLLAMA],
                label="Penyedia AI",
                value=AIProvider.XAI
            )
            
            with gr.Group() as api_settings:
                with gr.Row():
                    xai_key = gr.Textbox(
                        label="X.AI (Grok) API Key",
                        type="password",
                        placeholder="Opsional - Klik icon (?) untuk info. Kosongkan untuk gunakan key default",
                        show_label=True
                    )
                    gr.Markdown(XAI_API_HELP)
                
                with gr.Row():
                    gemini_key = gr.Textbox(
                        label="Gemini API Key",
                        type="password",
                        placeholder="Opsional - Kosongkan untuk gunakan key default",
                        show_label=True
                    )
                    gr.Markdown(GEMINI_API_HELP)

            # Model selection based on provider
            with gr.Row():
                model_dropdown = gr.Dropdown(
                    label="Model AI",
                    choices=XAI_MODELS,
                    value="grok-2-latest",
                    interactive=True
                )

            def update_model_list(provider_choice):
                if provider_choice == AIProvider.XAI:
                    return gr.Dropdown(choices=XAI_MODELS, value="grok-2-latest")
                elif provider_choice == AIProvider.GEMINI:
                    return gr.Dropdown(choices=GEMINI_MODELS, value="gemini-pro")
                else:  # OLLAMA
                    return gr.Dropdown(choices=OLLAMA_MODELS, value="llama2")
            
            provider.change(
                fn=update_model_list,
                inputs=[provider],
                outputs=[model_dropdown]
            )

        with gr.Tab("πŸ“Š Analisis Repository"):
            with gr.Row():
                repo_url = gr.Textbox(
                    label="URL Repository GitHub",
                    placeholder="Contoh: https://github.com/username/repository"
                )
                with gr.Column():
                    github_token = gr.Textbox(
                        label="Token GitHub", 
                        type="password",
                        placeholder="Klik icon (?) untuk panduan mendapatkan token"
                    )
                    gr.Markdown(GITHUB_TOKEN_HELP)
                branch = gr.Textbox(
                    label="Branch (opsional)",
                    placeholder="main"
                )
            
            clone_button = gr.Button("πŸ”„ Clone Repository", variant="primary")
            clone_status = gr.Markdown(
                label="Status Repository",
            )
            
            gr.Markdown("""
            ### πŸ’‘ Contoh Pertanyaan:
            - "Jelaskan struktur utama dari repository ini"
            - "Apa saja fitur-fitur utama dalam kode ini?"
            - "Bagaimana cara memperbaiki [masalah specific] di repository ini?"
            - "Tolong analisis kualitas kode di file [nama file]"
            """)
            
            with gr.Row():
                chat_input = gr.Textbox(
                    label="πŸ’­ Tanyakan tentang Repository",
                    placeholder="Ketik pertanyaan Anda di sini...",
                    lines=3
                )
                send_button = gr.Button("πŸ“€ Kirim", variant="primary")
            
            chat_history = gr.Chatbot(
                label="πŸ“ Riwayat Chat",
                height=500,
                show_label=True
            )

            async def handle_chat(message, history, provider_choice, model_name, xai_key, gemini_key):
                if not analyzer.current_repo:
                    yield history + [[message, "⚠️ Mohon clone repository terlebih dahulu sebelum mengajukan pertanyaan."]]
                    return
                
                history = history or []
                history.append([message, ""])
                
                full_response = ""
                if provider_choice == AIProvider.XAI:
                    async for chunk in analyzer.stream_xai_response(message, xai_key, model_name):
                        full_response += chunk
                        history[-1][1] = full_response
                        yield history
                elif provider_choice == AIProvider.GEMINI:
                    async for chunk in analyzer.stream_gemini_response(message, gemini_key or DEFAULT_GEMINI_KEY):
                        full_response += chunk
                        history[-1][1] = full_response
                        yield history
                else:  # OLLAMA
                    response = analyze_with_ollama(model_name, message)
                    history[-1][1] = response
                    yield history

            send_button.click(
                fn=handle_chat,
                inputs=[
                    chat_input,
                    chat_history,
                    provider,
                    model_dropdown,
                    xai_key,
                    gemini_key
                ],
                outputs=chat_history
            ).then(
                fn=lambda: gr.update(value=""),
                outputs=chat_input
            )

            chat_input.submit(
                fn=handle_chat,
                inputs=[
                    chat_input,
                    chat_history,
                    provider,
                    model_dropdown,
                    xai_key,
                    gemini_key
                ],
                outputs=chat_history
            ).then(
                fn=lambda: gr.update(value=""),
                outputs=chat_input
            )

    return app

if __name__ == "__main__":
    print(f"""
    πŸš€ Memulai Repository Chat Analysis
    πŸ“… Waktu: {CURRENT_TIME}
    πŸ‘€ Pengguna: {CURRENT_USER}
    """)
    
    app = create_ui()
    app.launch(share=True)