Spaces:
Sleeping
Sleeping
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() |