File size: 3,538 Bytes
e280a88
c0d25f6
e280a88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import openai
openai.api_key = os.getenv('api_token')
import gradio as gr
import re

def extract_information(log_file):
    # Read the log file and extract the lines containing errors or warnings
    with open(log_file.name, 'r') as f:
        lines = f.readlines()
        error_lines = [line.strip() for line in lines if re.search(r'\bERROR\b', line, re.IGNORECASE)]
        warning_lines = [line.strip() for line in lines if re.search(r'\bWARNING\b', line, re.IGNORECASE)]

    # Extract useful information from the error and warning lines
    errors = [re.findall(r'\bERROR:?\b (.+)', line, re.IGNORECASE)[0] if re.findall(r'\bERROR:?\b (.+)', line, re.IGNORECASE) else None for line in error_lines]
    warnings = [re.findall(r'\bWARNING:?\b (.+)', line, re.IGNORECASE)[0] if re.findall(r'\bWARNING:?\b (.+)', line, re.IGNORECASE) else None for line in warning_lines]

    # Remove any None values from the list of errors and warnings
    errors = [error for error in errors if error]
    warnings = [warning for warning in warnings if warning]

    # Return a dictionary of interesting information
    print (errors)
    print (warnings)
    return {'error_count': len(errors), 'warning_count': len(warnings), 'errors': errors, 'warnings': warnings}

def extract_information_log(log_file):
    # Read the log file and extract the lines containing errors or warnings
    # Open the log file for reading
    with open(log_file.name, 'r') as f:
    # Initialize an empty dictionary to store the errors and warnings
        errors_warnings = {}

    # Loop through each line in the log file
        for line in f:
        # Use regular expressions to extract the error or warning message
              match = re.search(r'(\w+): (.+)', line)
              if match:
                  level, message = match.groups()
                  if level == 'ERROR' or level == 'WARNING':
                # Add the error or warning message to the dictionary
                      if level not in errors_warnings:
                          errors_warnings[level] = []
                      errors_warnings[level].append(message.strip())

# Print the dictionary of errors and warnings
        print(errors_warnings)
        return errors_warnings


# Define a function to generate a story using OpenAI's GPT-3 API
def generate_story(info_dict):
    # Define the prompt for the GPT-3 API
    prompt = f"Based on the log file, there were {info_dict['ERROR']} errors and {info_dict['WARNING']} warnings. Generate an interesting story about how users might have been using this system that resulted in these errors\n\n"
    for i, error in enumerate(info_dict['ERROR']):
        prompt += f"Error {i+1}: {error}\n"
    for i, warning in enumerate(info_dict['WARNING']):
        prompt += f"Warning {i+1}: {warning}\n"

    # Generate a story using the GPT-3 API
    response = openai.Completion.create(
        engine="text-davinci-002",
        prompt=prompt,
        max_tokens=2048,
        n=1,
        stop=None,
        temperature=0.5,
    )

    # Return the generated story
    return response.choices[0].text

# Define the input and output interfaces for the Gradio app
inputs = [
    gr.inputs.File(label='Log File'),
]

output = gr.outputs.Textbox(label='Generated Story')

# Create the Gradio app and launch it
gradio_app = gr.Interface(fn=lambda log_file: generate_story(extract_information_log(log_file)), inputs=inputs, outputs=output, title='LogTales: Your Personal Storyteller created from logs')
gradio_app.launch(debug=True)