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)