coder2500/Algo
0
1import streamlit as st
2from transformers import pipeline
3from bigO import BigO
4
5# Load AI model for algorithm generation
6print("Loading AI model...")
7generator = pipeline("text-generation", model="gpt2") # Using GPT-2 for text generation
8print("AI model loaded successfully.")
9
10# Function to analyze time complexity
11def analyze_complexity(code):
12 print("Analyzing time complexity...")
13 lib = BigO() # Initialize BigO library for complexity analysis
14 result = lib.test(code, "random") # Test the provided function with random inputs
15 print("Time complexity analysis completed.")
16 return result
17
18# Example function to test time complexity
19def example_code(n):
20 print(f"Running example_code with n={n}")
21 for i in range(n): # Outer loop runs n times
22 for j in range(n): # Inner loop runs n times for each iteration of outer loop
23 pass # Simple operation, making this O(n^2)
24 print("example_code execution finished.")
25
26# Streamlit Web UI initialization
27print("Initializing Streamlit UI...")
28st.title("๐ AI Algorithm Generator") # Display title on web page
29st.write("Enter a problem statement, and the AI will generate multiple algorithms!") # Display instructions
30
31# Input text box for user to enter problem statement
32problem_statement = st.text_area("Problem Statement")
33
34# Button to generate algorithm when clicked
35if st.button("Generate Algorithm"):
36 print("Generating algorithm for input:", problem_statement)
37 output = generator(problem_statement, max_length=200) # Generate text based on problem statement
38 print("Algorithm generated successfully.")
39
40 # Display generated algorithm
41 st.write("### Suggested Algorithm:")
42 st.code(output[0]['generated_text'])
43
44 # Complexity Analysis
45 st.write("### Time Complexity Analysis:")
46 print("Performing complexity analysis...")
47 complexity = analyze_complexity(example_code) # Analyze example function's complexity
48 print("Complexity analysis result:", complexity)
49 st.write(complexity) # Display complexity result on the web page
50
51print("Streamlit UI ready.")
52 