import google.generativeai as genai import gradio as gr # Your API key (replace with a new one if this doesn't work) GOOGLE_API_KEY = "AIzaSyAVnkLjvUEZaQA5a-oUxcxb3bZ5amZDYqM" # Configure API genai.configure(api_key=GOOGLE_API_KEY) # Use the free model model = genai.GenerativeModel('gemini-1.5-flash-latest') def generate_recommendation(problem_type, dataset_size, num_features, feature_type, priority, additional_info): prompt = f""" You are an expert machine learning engineer specializing in algorithm selection. Recommend machine learning algorithms for a project with these characteristics: 1. Problem Type: {problem_type} 2. Dataset Size: {dataset_size} 3. Number of Features: {num_features} 4. Feature Types: {feature_type} 5. Priority: {priority} 6. Additional Information: {additional_info} Provide: 1. Top 3 ranked algorithm recommendations (most suitable first) 2. For each algorithm: - Brief justification - Strengths for this use case - Potential limitations 3. Final recommendation with detailed comparison Format exactly like this: === TOP RECOMMENDATIONS === 1. [Algorithm 1] - Why: [Justification] - Pros: [Strengths] - Cons: [Limitations] 2. [Algorithm 2] - Why: [Justification] - Pros: [Strengths] - Cons: [Limitations] 3. [Algorithm 3] - Why: [Justification] - Pros: [Strengths] - Cons: [Limitations] === FINAL CHOICE === Best Algorithm: [Algorithm Name] - Why Best: [Detailed comparison] - Why Others Are Less Suitable: [Explanation] """ try: response = model.generate_content(prompt) return response.text except Exception as e: return f"Error: {e}\n\nTip: The API key may need enabling at https://aistudio.google.com/" # Create Gradio interface with gr.Blocks(title="ML Algorithm Recommender", theme=gr.themes.Soft()) as demo: gr.Markdown(""" # Machine Learning Algorithm Recommender Enter your project characteristics to get personalized algorithm recommendations """) with gr.Row(): with gr.Column(): problem_type = gr.Textbox(label="Problem Type*", placeholder="classification, regression, clustering...") dataset_size = gr.Textbox(label="Dataset Size*", placeholder="small, medium, large or specific number") num_features = gr.Textbox(label="Number of Features*", placeholder="few, many, or specific number") feature_type = gr.Textbox(label="Feature Types*", placeholder="numerical, categorical, mixed") priority = gr.Textbox(label="Priority*", placeholder="accuracy, speed, interpretability") additional_info = gr.Textbox(label="Additional Details (optional)", placeholder="Any other important information") submit_btn = gr.Button("Get Recommendations", variant="primary") with gr.Column(): output = gr.Textbox(label="Recommendation Results", lines=20, interactive=False) submit_btn.click( fn=generate_recommendation, inputs=[problem_type, dataset_size, num_features, feature_type, priority, additional_info], outputs=output ) if __name__ == "__main__": demo.launch()