Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,15 +8,23 @@ from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
|
|
| 8 |
from langchain_community.llms import HuggingFacePipeline
|
| 9 |
from typing import List, Dict
|
| 10 |
import json
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
# Configure logging
|
| 13 |
logging.basicConfig(level=logging.INFO)
|
| 14 |
logger = logging.getLogger(__name__)
|
| 15 |
|
| 16 |
# Global variables
|
| 17 |
-
CURRENT_UTC_TIME = datetime.strptime("2025-02-04 19:
|
| 18 |
CURRENT_USER = "ChadSaglam"
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
@st.cache_resource
|
| 21 |
def load_ai_model():
|
| 22 |
try:
|
|
@@ -28,11 +36,12 @@ def load_ai_model():
|
|
| 28 |
"text-generation",
|
| 29 |
model=model,
|
| 30 |
tokenizer=tokenizer,
|
| 31 |
-
max_new_tokens=
|
| 32 |
do_sample=True,
|
| 33 |
temperature=0.7,
|
| 34 |
top_p=0.95,
|
| 35 |
-
repetition_penalty=1.15
|
|
|
|
| 36 |
)
|
| 37 |
|
| 38 |
return HuggingFacePipeline(pipeline=pipe)
|
|
@@ -40,74 +49,112 @@ def load_ai_model():
|
|
| 40 |
logger.error(f"Error loading AI model: {str(e)}")
|
| 41 |
return None
|
| 42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
class AIProjectManager:
|
| 44 |
def __init__(self):
|
| 45 |
self.llm = load_ai_model()
|
| 46 |
-
self.
|
| 47 |
-
"E-commerce Website",
|
| 48 |
-
"Mobile Application",
|
| 49 |
-
"SaaS Platform",
|
| 50 |
-
"AI/ML Solution",
|
| 51 |
-
"Enterprise System",
|
| 52 |
-
"Blockchain Platform"
|
| 53 |
-
]
|
| 54 |
|
| 55 |
-
self.
|
| 56 |
-
"
|
| 57 |
-
"
|
| 58 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
]
|
| 60 |
|
| 61 |
-
def
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
"
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
|
| 106 |
-
|
| 107 |
-
|
|
|
|
|
|
|
|
|
|
| 108 |
|
| 109 |
-
def
|
| 110 |
-
"""Format a single task with required fields"""
|
| 111 |
return {
|
| 112 |
"id": str(uuid.uuid4()),
|
| 113 |
"title": task.get("title", "Untitled Task"),
|
|
@@ -129,74 +176,39 @@ class AIProjectManager:
|
|
| 129 |
]
|
| 130 |
}
|
| 131 |
|
| 132 |
-
def generate_project_tasks(self, project_name: str, project_type: str) -> List[Dict]:
|
| 133 |
-
try:
|
| 134 |
-
prompt = f"""
|
| 135 |
-
Create a detailed project plan for {project_name} ({project_type}) with the following JSON structure:
|
| 136 |
-
{{
|
| 137 |
-
"tasks": [
|
| 138 |
-
{{
|
| 139 |
-
"title": "Task name",
|
| 140 |
-
"priority": "P0/P1/P2/P3",
|
| 141 |
-
"assigned_to": "Team/Role",
|
| 142 |
-
"estimated_hours": number,
|
| 143 |
-
"description": "Detailed description",
|
| 144 |
-
"acceptance_criteria": ["criteria1", "criteria2"],
|
| 145 |
-
"subtasks": [
|
| 146 |
-
{{
|
| 147 |
-
"title": "Subtask name",
|
| 148 |
-
"assigned_to": "Team/Role",
|
| 149 |
-
"estimated_hours": number
|
| 150 |
-
}}
|
| 151 |
-
]
|
| 152 |
-
}}
|
| 153 |
-
]
|
| 154 |
-
}}
|
| 155 |
-
"""
|
| 156 |
-
|
| 157 |
-
# Generate response using the AI model
|
| 158 |
-
response = self.llm(prompt)
|
| 159 |
-
|
| 160 |
-
try:
|
| 161 |
-
# Try to parse the AI response as JSON
|
| 162 |
-
tasks_data = json.loads(response)
|
| 163 |
-
return [self._format_single_task(task) for task in tasks_data.get("tasks", [])]
|
| 164 |
-
except json.JSONDecodeError:
|
| 165 |
-
# Fallback to structured tasks if JSON parsing fails
|
| 166 |
-
logger.warning("Failed to parse AI response, using structured tasks")
|
| 167 |
-
return self._generate_structured_tasks(project_name, project_type)
|
| 168 |
-
|
| 169 |
-
except Exception as e:
|
| 170 |
-
logger.error(f"Error generating tasks: {str(e)}")
|
| 171 |
-
return self._generate_structured_tasks(project_name, project_type)
|
| 172 |
-
|
| 173 |
def render_task_ui():
|
| 174 |
st.title("AI-Powered Project Management")
|
| 175 |
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
"
|
| 179 |
-
|
| 180 |
-
"
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
|
|
|
|
|
|
|
|
|
| 185 |
|
| 186 |
if st.button("Generate Tasks") and project_name:
|
| 187 |
-
with st.spinner("Generating
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
|
|
|
|
|
|
|
|
|
| 193 |
|
| 194 |
def display_tasks(tasks: List[Dict]):
|
| 195 |
st.subheader("Project Tasks")
|
| 196 |
|
| 197 |
for task in tasks:
|
| 198 |
with st.expander(f"π {task['title']} ({task['priority']})"):
|
| 199 |
-
col1, col2 = st.columns(
|
| 200 |
|
| 201 |
with col1:
|
| 202 |
st.write("**Description:**")
|
|
@@ -208,9 +220,10 @@ def display_tasks(tasks: List[Dict]):
|
|
| 208 |
|
| 209 |
with col2:
|
| 210 |
st.write("**Details:**")
|
| 211 |
-
st.write(f"Assigned to: {task['assigned_to']}")
|
| 212 |
-
st.write(f"
|
| 213 |
-
|
|
|
|
| 214 |
|
| 215 |
if task["subtasks"]:
|
| 216 |
st.write("**Subtasks:**")
|
|
@@ -225,7 +238,12 @@ def display_tasks(tasks: List[Dict]):
|
|
| 225 |
subtask["status"] = "TO_DO"
|
| 226 |
|
| 227 |
def main():
|
| 228 |
-
st.set_page_config(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 229 |
|
| 230 |
st.sidebar.info(f"Current User: {CURRENT_USER}")
|
| 231 |
st.sidebar.info(f"Current Time (UTC): {CURRENT_UTC_TIME.strftime('%Y-%m-%d %H:%M:%S')}")
|
|
|
|
| 8 |
from langchain_community.llms import HuggingFacePipeline
|
| 9 |
from typing import List, Dict
|
| 10 |
import json
|
| 11 |
+
import timeout_decorator
|
| 12 |
+
import threading
|
| 13 |
+
from queue import Queue
|
| 14 |
+
import time
|
| 15 |
|
| 16 |
# Configure logging
|
| 17 |
logging.basicConfig(level=logging.INFO)
|
| 18 |
logger = logging.getLogger(__name__)
|
| 19 |
|
| 20 |
# Global variables
|
| 21 |
+
CURRENT_UTC_TIME = datetime.strptime("2025-02-04 19:37:20", "%Y-%m-%d %H:%M:%S")
|
| 22 |
CURRENT_USER = "ChadSaglam"
|
| 23 |
|
| 24 |
+
# Constants
|
| 25 |
+
MODEL_TIMEOUT = 30 # seconds
|
| 26 |
+
DEFAULT_MAX_TOKENS = 256
|
| 27 |
+
|
| 28 |
@st.cache_resource
|
| 29 |
def load_ai_model():
|
| 30 |
try:
|
|
|
|
| 36 |
"text-generation",
|
| 37 |
model=model,
|
| 38 |
tokenizer=tokenizer,
|
| 39 |
+
max_new_tokens=DEFAULT_MAX_TOKENS,
|
| 40 |
do_sample=True,
|
| 41 |
temperature=0.7,
|
| 42 |
top_p=0.95,
|
| 43 |
+
repetition_penalty=1.15,
|
| 44 |
+
num_return_sequences=1
|
| 45 |
)
|
| 46 |
|
| 47 |
return HuggingFacePipeline(pipeline=pipe)
|
|
|
|
| 49 |
logger.error(f"Error loading AI model: {str(e)}")
|
| 50 |
return None
|
| 51 |
|
| 52 |
+
class TaskGenerator:
|
| 53 |
+
@timeout_decorator.timeout(MODEL_TIMEOUT)
|
| 54 |
+
def generate_with_timeout(self, llm, prompt):
|
| 55 |
+
return llm.invoke(prompt)
|
| 56 |
+
|
| 57 |
class AIProjectManager:
|
| 58 |
def __init__(self):
|
| 59 |
self.llm = load_ai_model()
|
| 60 |
+
self.task_generator = TaskGenerator()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
+
self.project_types = {
|
| 63 |
+
"E-commerce Website": self._get_ecommerce_template,
|
| 64 |
+
"Mobile Application": self._get_mobile_template,
|
| 65 |
+
"SaaS Platform": self._get_saas_template,
|
| 66 |
+
"AI/ML Solution": self._get_ai_template,
|
| 67 |
+
"Enterprise System": self._get_enterprise_template,
|
| 68 |
+
"Blockchain Platform": self._get_blockchain_template
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
def _get_base_template(self) -> List[Dict]:
|
| 72 |
+
return [
|
| 73 |
+
{
|
| 74 |
+
"title": "Project Setup",
|
| 75 |
+
"priority": "P0",
|
| 76 |
+
"assigned_to": "Tech Lead",
|
| 77 |
+
"estimated_hours": 24,
|
| 78 |
+
"description": "Initial project setup and configuration",
|
| 79 |
+
"acceptance_criteria": ["Repository setup", "CI/CD pipeline", "Development environment"],
|
| 80 |
+
"subtasks": [
|
| 81 |
+
{"title": "Repository Setup", "assigned_to": "DevOps", "estimated_hours": 8},
|
| 82 |
+
{"title": "CI/CD Configuration", "assigned_to": "DevOps", "estimated_hours": 16}
|
| 83 |
+
]
|
| 84 |
+
}
|
| 85 |
]
|
| 86 |
|
| 87 |
+
def _get_ecommerce_template(self) -> List[Dict]:
|
| 88 |
+
base = self._get_base_template()
|
| 89 |
+
base.extend([
|
| 90 |
+
{
|
| 91 |
+
"title": "Product Catalog",
|
| 92 |
+
"priority": "P0",
|
| 93 |
+
"assigned_to": "Frontend Team",
|
| 94 |
+
"estimated_hours": 40,
|
| 95 |
+
"description": "Implement product catalog features",
|
| 96 |
+
"acceptance_criteria": ["Product listing", "Search", "Filters"],
|
| 97 |
+
"subtasks": [
|
| 98 |
+
{"title": "Product Grid", "assigned_to": "Frontend Dev", "estimated_hours": 16},
|
| 99 |
+
{"title": "Search Implementation", "assigned_to": "Frontend Dev", "estimated_hours": 24}
|
| 100 |
+
]
|
| 101 |
+
}
|
| 102 |
+
])
|
| 103 |
+
return base
|
| 104 |
+
|
| 105 |
+
def _get_mobile_template(self) -> List[Dict]:
|
| 106 |
+
base = self._get_base_template()
|
| 107 |
+
base.extend([
|
| 108 |
+
{
|
| 109 |
+
"title": "Mobile App Structure",
|
| 110 |
+
"priority": "P0",
|
| 111 |
+
"assigned_to": "Mobile Team",
|
| 112 |
+
"estimated_hours": 40,
|
| 113 |
+
"description": "Setup mobile app architecture",
|
| 114 |
+
"acceptance_criteria": ["Navigation", "State management", "API integration"],
|
| 115 |
+
"subtasks": [
|
| 116 |
+
{"title": "Navigation Setup", "assigned_to": "Mobile Dev", "estimated_hours": 16},
|
| 117 |
+
{"title": "State Management", "assigned_to": "Mobile Dev", "estimated_hours": 24}
|
| 118 |
+
]
|
| 119 |
+
}
|
| 120 |
+
])
|
| 121 |
+
return base
|
| 122 |
+
|
| 123 |
+
def generate_project_tasks(self, project_name: str, project_type: str) -> List[Dict]:
|
| 124 |
+
try:
|
| 125 |
+
# Get template tasks based on project type
|
| 126 |
+
template_getter = self.project_types.get(project_type, self._get_base_template)
|
| 127 |
+
base_tasks = template_getter()
|
| 128 |
+
|
| 129 |
+
# Try to enhance tasks with AI
|
| 130 |
+
try:
|
| 131 |
+
enhanced_tasks = self._enhance_tasks_with_ai(project_name, project_type, base_tasks)
|
| 132 |
+
return enhanced_tasks
|
| 133 |
+
except (timeout_decorator.TimeoutError, Exception) as e:
|
| 134 |
+
logger.warning(f"AI enhancement failed: {str(e)}, using template tasks")
|
| 135 |
+
return [self._format_task(task) for task in base_tasks]
|
| 136 |
+
|
| 137 |
+
except Exception as e:
|
| 138 |
+
logger.error(f"Error generating tasks: {str(e)}")
|
| 139 |
+
return [self._format_task(task) for task in self._get_base_template()]
|
| 140 |
+
|
| 141 |
+
def _enhance_tasks_with_ai(self, project_name: str, project_type: str, base_tasks: List[Dict]) -> List[Dict]:
|
| 142 |
+
prompt = f"""
|
| 143 |
+
Enhance these tasks for {project_name} ({project_type}):
|
| 144 |
+
{json.dumps(base_tasks, indent=2)}
|
| 145 |
+
|
| 146 |
+
Add more specific details and subtasks while maintaining the same JSON structure.
|
| 147 |
+
"""
|
| 148 |
+
|
| 149 |
+
result = self.task_generator.generate_with_timeout(self.llm, prompt)
|
| 150 |
|
| 151 |
+
try:
|
| 152 |
+
enhanced = json.loads(result)
|
| 153 |
+
return [self._format_task(task) for task in enhanced]
|
| 154 |
+
except:
|
| 155 |
+
return [self._format_task(task) for task in base_tasks]
|
| 156 |
|
| 157 |
+
def _format_task(self, task: Dict) -> Dict:
|
|
|
|
| 158 |
return {
|
| 159 |
"id": str(uuid.uuid4()),
|
| 160 |
"title": task.get("title", "Untitled Task"),
|
|
|
|
| 176 |
]
|
| 177 |
}
|
| 178 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
def render_task_ui():
|
| 180 |
st.title("AI-Powered Project Management")
|
| 181 |
|
| 182 |
+
col1, col2 = st.columns([2, 1])
|
| 183 |
+
with col1:
|
| 184 |
+
project_name = st.text_input("Project Name", placeholder="Enter your project name")
|
| 185 |
+
with col2:
|
| 186 |
+
project_type = st.selectbox("Project Type", [
|
| 187 |
+
"E-commerce Website",
|
| 188 |
+
"Mobile Application",
|
| 189 |
+
"SaaS Platform",
|
| 190 |
+
"AI/ML Solution",
|
| 191 |
+
"Enterprise System",
|
| 192 |
+
"Blockchain Platform"
|
| 193 |
+
])
|
| 194 |
|
| 195 |
if st.button("Generate Tasks") and project_name:
|
| 196 |
+
with st.spinner("Generating project tasks..."):
|
| 197 |
+
try:
|
| 198 |
+
ai_manager = AIProjectManager()
|
| 199 |
+
tasks = ai_manager.generate_project_tasks(project_name, project_type)
|
| 200 |
+
st.session_state.tasks = tasks
|
| 201 |
+
st.success("Tasks generated successfully!")
|
| 202 |
+
display_tasks(tasks)
|
| 203 |
+
except Exception as e:
|
| 204 |
+
st.error(f"Error generating tasks: {str(e)}")
|
| 205 |
|
| 206 |
def display_tasks(tasks: List[Dict]):
|
| 207 |
st.subheader("Project Tasks")
|
| 208 |
|
| 209 |
for task in tasks:
|
| 210 |
with st.expander(f"π {task['title']} ({task['priority']})"):
|
| 211 |
+
col1, col2 = st.columns([3, 1])
|
| 212 |
|
| 213 |
with col1:
|
| 214 |
st.write("**Description:**")
|
|
|
|
| 220 |
|
| 221 |
with col2:
|
| 222 |
st.write("**Details:**")
|
| 223 |
+
st.write(f"π§βπ» Assigned to: {task['assigned_to']}")
|
| 224 |
+
st.write(f"β±οΈ Est. hours: {task['estimated_hours']}")
|
| 225 |
+
status_color = "π’" if task["status"] == "DONE" else "π΄"
|
| 226 |
+
st.write(f"{status_color} Status: {task['status']}")
|
| 227 |
|
| 228 |
if task["subtasks"]:
|
| 229 |
st.write("**Subtasks:**")
|
|
|
|
| 238 |
subtask["status"] = "TO_DO"
|
| 239 |
|
| 240 |
def main():
|
| 241 |
+
st.set_page_config(
|
| 242 |
+
page_title="AI Project Management",
|
| 243 |
+
page_icon="π",
|
| 244 |
+
layout="wide",
|
| 245 |
+
initial_sidebar_state="expanded"
|
| 246 |
+
)
|
| 247 |
|
| 248 |
st.sidebar.info(f"Current User: {CURRENT_USER}")
|
| 249 |
st.sidebar.info(f"Current Time (UTC): {CURRENT_UTC_TIME.strftime('%Y-%m-%d %H:%M:%S')}")
|