acecalisto3 commited on
Commit
c0311b6
1 Parent(s): d8ebd13

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +120 -51
app.py CHANGED
@@ -1,54 +1,123 @@
1
- import streamlit as st
2
- from huggingface_hub import HfApi
3
  import os
4
- import gradio as gr
5
- from transformers import AutoTokenizer, AutoModelForCausalLM
6
- from typing import List, Dict
7
- from supplemental import Agent, Tool, CodeGenerationTool, DataRetrievalTool, TextGenerationTool, CodeExecutionTool, CodeDebuggingTool, CodeSummarizationTool, CodeTranslationTool, CodeOptimizationTool, CodeDocumentationTool, ImageGenerationTool, ImageEditingTool, ImageAnalysisTool, Workflow, EnhancedAIAgent
8
-
9
- st.title("CODEFUSSION ☄")
10
-
11
- # Access Hugging Face API key from secrets
12
- hf_token = st.secrets["hf_token"]
13
- if not hf_token:
14
- st.error("Hugging Face API key not found. Please make sure it is set in the secrets.")
15
-
16
- # --- Agent Pool ---
17
- agent_pool = {
18
- "IdeaIntake": EnhancedAIAgent("IdeaIntake", "Idea Intake", ["Data Retrieval", "Code Generation", "Text Generation"], "bigcode/starcoder"),
19
- "CodeBuilder": EnhancedAIAgent("CodeBuilder", "Code Builder", ["Code Generation", "Code Debugging", "Code Optimization"], "bigcode/starcoder"),
20
- "ImageCreator": EnhancedAIAgent("ImageCreator", "Image Creator", ["Image Generation", "Image Editing"], "bigcode/starcoder"),
21
- }
22
-
23
- # --- Workflow Definitions ---
24
- class Workflow:
25
- def __init__(self, name, agents, task, description):
26
- self.name = name
27
- self.agents = agents
28
- self.task = task
29
- self.description = description
30
-
31
- def run(self, prompt, context):
32
- # Workflow execution logic
33
- for agent in self.agents:
34
- action = agent.act(prompt, context)
35
- # Execute the tool
36
- if action.get("tool"):
37
- tool = next((t for t in agent.tools if t.name == action["tool"]), None)
38
- if tool:
39
- output = tool.run(action["arguments"])
40
- # Update context
41
- context.update(output)
42
-
43
- # Example usage
44
- workflow = Workflow(
45
- name="Example Workflow",
46
- agents=[agent_pool["IdeaIntake"], agent_pool["CodeBuilder"]],
47
- task="Generate and debug code",
48
- description="A workflow to generate and debug code using AI agents."
49
  )
50
 
51
- context = {}
52
- prompt = "Create a Python function to add two numbers."
53
- workflow.run(prompt, context)
54
- st.write(context)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import os
2
+ import logging
3
+ from flask import Flask, request, jsonify, render_template
4
+ from dotenv import load_dotenv
5
+ from supplemental import EnhancedAIAgent, ProjectConfig
6
+
7
+ # Load environment variables
8
+ load_dotenv()
9
+
10
+ # Configure logging
11
+ logging.basicConfig(level=logging.INFO)
12
+ logger = logging.getLogger(__name__)
13
+
14
+ # Initialize Flask app
15
+ app = Flask(__name__)
16
+
17
+ # Initialize EnhancedAIAgent
18
+ agent = EnhancedAIAgent(
19
+ name="WebDevMaster",
20
+ description="Expert in full-stack web development",
21
+ skills=["HTML", "CSS", "JavaScript", "React", "Node.js"],
22
+ model_name="gpt2"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  )
24
 
25
+ @app.route('/')
26
+ def index():
27
+ return render_template('index.html')
28
+
29
+ @app.route('/generate_project_config', methods=['POST'])
30
+ def generate_project_config():
31
+ try:
32
+ project_description = request.json['project_description']
33
+ config = agent.generate_project_config(project_description)
34
+ return jsonify(config.__dict__), 200
35
+ except Exception as e:
36
+ logger.error(f"Error generating project config: {str(e)}")
37
+ return jsonify({"error": str(e)}), 400
38
+
39
+ @app.route('/create_project_structure', methods=['POST'])
40
+ def create_project_structure():
41
+ try:
42
+ config_dict = request.json
43
+ config = ProjectConfig(**config_dict)
44
+ structure = agent.create_project_structure(config)
45
+ return jsonify(structure), 200
46
+ except Exception as e:
47
+ logger.error(f"Error creating project structure: {str(e)}")
48
+ return jsonify({"error": str(e)}), 400
49
+
50
+ @app.route('/implement_feature', methods=['POST'])
51
+ def implement_feature():
52
+ try:
53
+ feature_description = request.json['feature_description']
54
+ existing_code = request.json.get('existing_code')
55
+ new_code = agent.implement_feature(feature_description, existing_code)
56
+ return jsonify({"code": new_code}), 200
57
+ except Exception as e:
58
+ logger.error(f"Error implementing feature: {str(e)}")
59
+ return jsonify({"error": str(e)}), 400
60
+
61
+ @app.route('/review_code', methods=['POST'])
62
+ def review_code():
63
+ try:
64
+ code = request.json['code']
65
+ review = agent.review_code(code)
66
+ return jsonify({"review": review}), 200
67
+ except Exception as e:
68
+ logger.error(f"Error reviewing code: {str(e)}")
69
+ return jsonify({"error": str(e)}), 400
70
+
71
+ @app.route('/optimize_code', methods=['POST'])
72
+ def optimize_code():
73
+ try:
74
+ code = request.json['code']
75
+ optimization_goal = request.json['optimization_goal']
76
+ optimized_code = agent.optimize_code(code, optimization_goal)
77
+ return jsonify({"optimized_code": optimized_code}), 200
78
+ except Exception as e:
79
+ logger.error(f"Error optimizing code: {str(e)}")
80
+ return jsonify({"error": str(e)}), 400
81
+
82
+ @app.route('/generate_documentation', methods=['POST'])
83
+ def generate_documentation():
84
+ try:
85
+ code = request.json['code']
86
+ documentation = agent.generate_documentation(code)
87
+ return jsonify({"documentation": documentation}), 200
88
+ except Exception as e:
89
+ logger.error(f"Error generating documentation: {str(e)}")
90
+ return jsonify({"error": str(e)}), 400
91
+
92
+ @app.route('/suggest_tests', methods=['POST'])
93
+ def suggest_tests():
94
+ try:
95
+ code = request.json['code']
96
+ test_suggestions = agent.suggest_tests(code)
97
+ return jsonify({"test_suggestions": test_suggestions}), 200
98
+ except Exception as e:
99
+ logger.error(f"Error suggesting tests: {str(e)}")
100
+ return jsonify({"error": str(e)}), 400
101
+
102
+ @app.route('/explain_code', methods=['POST'])
103
+ def explain_code():
104
+ try:
105
+ code = request.json['code']
106
+ explanation = agent.explain_code(code)
107
+ return jsonify({"explanation": explanation}), 200
108
+ except Exception as e:
109
+ logger.error(f"Error explaining code: {str(e)}")
110
+ return jsonify({"error": str(e)}), 400
111
+
112
+ @app.route('/suggest_refactoring', methods=['POST'])
113
+ def suggest_refactoring():
114
+ try:
115
+ code = request.json['code']
116
+ refactoring_suggestions = agent.suggest_refactoring(code)
117
+ return jsonify({"refactoring_suggestions": refactoring_suggestions}), 200
118
+ except Exception as e:
119
+ logger.error(f"Error suggesting refactoring: {str(e)}")
120
+ return jsonify({"error": str(e)}), 400
121
+
122
+ if __name__ == '__main__':
123
+ app.run(debug=os.getenv('FLASK_DEBUG', 'False') == 'True', host='0.0.0.0', port=int(os.getenv('PORT', 5000)))