File size: 2,803 Bytes
0adea63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from llama_cpp import Llama
from pygrok import Grok
from typing import Optional, Dict, Union
from huggingface_hub import hf_hub_download
import gradio as gr
import time


pattern_counter = 0

def parse_log_with_grok(log_line: str, grok_pattern: str) -> Optional[Dict[str, Union[str, int, float]]]:
    try:
        grok = Grok(grok_pattern)
        match = grok.match(log_line)
        return match if match else None
    except Exception as e:
        raise ValueError(f"Grok pattern işlenirken hata oluştu: {str(e)}")

model_path = hf_hub_download(
    repo_id="omeryentur/gemma-2-2b-it-grok-2-gguf",
    filename="gemma2-2b-it-grokpattern.Q4_K_M.gguf",
    use_auth_token=True
)

llm = Llama(
    model_path=model_path,
    n_ctx=512,
    n_threads=1,
)

def generate_pattern(text: str, progress=gr.Progress()):
    global pattern_counter
    
    if not text:
        return {"error": "Lütfen bir metin girin!"}, None, None
    
    try:
        prompt = f"""<start_of_turn>log{text}<end_of_turn><start_of_turn>model"""
        
        for i in range(3):
            progress(i/3, desc=f"Pattern {i+1}/3 oluşturuluyor...")
            
            completion = llm(
                prompt,
                max_tokens=512,
                temperature=i/10,
                stop=["<end_of_turn>"]
            )
            
            generated_pattern = completion['choices'][0]['text'].strip()
            result = parse_log_with_grok(text, generated_pattern)
            
            if result:
                pattern_counter += 1
                pattern = {
                    "log": text,
                    "pattern": generated_pattern,
                }
                return pattern, result, f"Oluşturulan Pattern Sayısı: {pattern_counter}"
                
            time.sleep(0.5)
            
        return {"error": "Uygun pattern oluşturulamadı"}, None, f"Oluşturulan Pattern Sayısı: {pattern_counter}"
        
    except Exception as e:
        return {"error": f"Bir hata oluştu: {str(e)}"}, None, f"Oluşturulan Pattern Sayısı: {pattern_counter}"

# Create Gradio interface
with gr.Blocks() as demo:
    gr.Markdown("# Log Grok Pattern")
    
    with gr.Row():
        with gr.Column():
            text_input = gr.Textbox(label="Log girin:")
            generate_btn = gr.Button("Oluştur")
            pattern_count = gr.Markdown(f"### Oluşturulan Pattern Sayısı: {pattern_counter}")
        
    with gr.Row():
        with gr.Column():
            pattern_output = gr.JSON(label="Log and Pattern")
            result_output = gr.JSON(label="Result")
    
    generate_btn.click(
        fn=generate_pattern,
        inputs=[text_input],
        outputs=[pattern_output, result_output, pattern_count]
    )

if __name__ == "__main__":
    demo.launch()