Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from llama_cpp import Llama
|
2 |
+
from pygrok import Grok
|
3 |
+
from typing import Optional, Dict, Union
|
4 |
+
from huggingface_hub import hf_hub_download
|
5 |
+
import gradio as gr
|
6 |
+
import time
|
7 |
+
|
8 |
+
|
9 |
+
pattern_counter = 0
|
10 |
+
|
11 |
+
def parse_log_with_grok(log_line: str, grok_pattern: str) -> Optional[Dict[str, Union[str, int, float]]]:
|
12 |
+
try:
|
13 |
+
grok = Grok(grok_pattern)
|
14 |
+
match = grok.match(log_line)
|
15 |
+
return match if match else None
|
16 |
+
except Exception as e:
|
17 |
+
raise ValueError(f"Grok pattern işlenirken hata oluştu: {str(e)}")
|
18 |
+
|
19 |
+
model_path = hf_hub_download(
|
20 |
+
repo_id="omeryentur/gemma-2-2b-it-grok-2-gguf",
|
21 |
+
filename="gemma2-2b-it-grokpattern.Q4_K_M.gguf",
|
22 |
+
use_auth_token=True
|
23 |
+
)
|
24 |
+
|
25 |
+
llm = Llama(
|
26 |
+
model_path=model_path,
|
27 |
+
n_ctx=512,
|
28 |
+
n_threads=1,
|
29 |
+
)
|
30 |
+
|
31 |
+
def generate_pattern(text: str, progress=gr.Progress()):
|
32 |
+
global pattern_counter
|
33 |
+
|
34 |
+
if not text:
|
35 |
+
return {"error": "Lütfen bir metin girin!"}, None, None
|
36 |
+
|
37 |
+
try:
|
38 |
+
prompt = f"""<start_of_turn>log{text}<end_of_turn><start_of_turn>model"""
|
39 |
+
|
40 |
+
for i in range(3):
|
41 |
+
progress(i/3, desc=f"Pattern {i+1}/3 oluşturuluyor...")
|
42 |
+
|
43 |
+
completion = llm(
|
44 |
+
prompt,
|
45 |
+
max_tokens=512,
|
46 |
+
temperature=i/10,
|
47 |
+
stop=["<end_of_turn>"]
|
48 |
+
)
|
49 |
+
|
50 |
+
generated_pattern = completion['choices'][0]['text'].strip()
|
51 |
+
result = parse_log_with_grok(text, generated_pattern)
|
52 |
+
|
53 |
+
if result:
|
54 |
+
pattern_counter += 1
|
55 |
+
pattern = {
|
56 |
+
"log": text,
|
57 |
+
"pattern": generated_pattern,
|
58 |
+
}
|
59 |
+
return pattern, result, f"Oluşturulan Pattern Sayısı: {pattern_counter}"
|
60 |
+
|
61 |
+
time.sleep(0.5)
|
62 |
+
|
63 |
+
return {"error": "Uygun pattern oluşturulamadı"}, None, f"Oluşturulan Pattern Sayısı: {pattern_counter}"
|
64 |
+
|
65 |
+
except Exception as e:
|
66 |
+
return {"error": f"Bir hata oluştu: {str(e)}"}, None, f"Oluşturulan Pattern Sayısı: {pattern_counter}"
|
67 |
+
|
68 |
+
# Create Gradio interface
|
69 |
+
with gr.Blocks() as demo:
|
70 |
+
gr.Markdown("# Log Grok Pattern")
|
71 |
+
|
72 |
+
with gr.Row():
|
73 |
+
with gr.Column():
|
74 |
+
text_input = gr.Textbox(label="Log girin:")
|
75 |
+
generate_btn = gr.Button("Oluştur")
|
76 |
+
pattern_count = gr.Markdown(f"### Oluşturulan Pattern Sayısı: {pattern_counter}")
|
77 |
+
|
78 |
+
with gr.Row():
|
79 |
+
with gr.Column():
|
80 |
+
pattern_output = gr.JSON(label="Log and Pattern")
|
81 |
+
result_output = gr.JSON(label="Result")
|
82 |
+
|
83 |
+
generate_btn.click(
|
84 |
+
fn=generate_pattern,
|
85 |
+
inputs=[text_input],
|
86 |
+
outputs=[pattern_output, result_output, pattern_count]
|
87 |
+
)
|
88 |
+
|
89 |
+
if __name__ == "__main__":
|
90 |
+
demo.launch()
|