Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,149 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
3 |
+
import torch
|
4 |
+
import json
|
5 |
+
from datetime import datetime
|
6 |
+
|
7 |
+
# Load Llama 3 model (quantized for CPU hosting)
|
8 |
+
MODEL_NAME = "meta-llama/Meta-Llama-3-8B-Instruct"
|
9 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
10 |
+
model = AutoModelForCausalLM.from_pretrained(MODEL_NAME, torch_dtype=torch.float16, device_map="auto")
|
11 |
+
|
12 |
+
# Load Llama Guard for content moderation
|
13 |
+
LLAMA_GUARD_NAME = "meta-llama/Llama-Guard-3-1B-INT4"
|
14 |
+
guard_tokenizer = AutoTokenizer.from_pretrained(LLAMA_GUARD_NAME)
|
15 |
+
guard_model = AutoModelForCausalLM.from_pretrained(LLAMA_GUARD_NAME, torch_dtype=torch.float16, device_map="auto")
|
16 |
+
|
17 |
+
# Define Prompt Templates
|
18 |
+
PROMPTS = {
|
19 |
+
"project_analysis": """Analyze this project description and generate:
|
20 |
+
1. Project timeline with milestones
|
21 |
+
2. Required technology stack
|
22 |
+
3. Potential risks
|
23 |
+
4. Team composition
|
24 |
+
5. Cost estimation
|
25 |
+
|
26 |
+
Project: {project_description}""",
|
27 |
+
|
28 |
+
"code_generation": """Generate implementation code for this feature:
|
29 |
+
{feature_description}
|
30 |
+
|
31 |
+
Considerations:
|
32 |
+
- Use {programming_language}
|
33 |
+
- Follow {coding_standards}
|
34 |
+
- Include error handling
|
35 |
+
- Add documentation""",
|
36 |
+
|
37 |
+
"risk_analysis": """Predict potential risks for this project plan:
|
38 |
+
{project_data}
|
39 |
+
|
40 |
+
Format output as JSON with risk types, probabilities, and mitigation strategies"""
|
41 |
+
}
|
42 |
+
|
43 |
+
# Function: Content Moderation using Llama Guard
|
44 |
+
def moderate_input(user_input):
|
45 |
+
inputs = guard_tokenizer(user_input, return_tensors="pt", max_length=512, truncation=True)
|
46 |
+
outputs = guard_model.generate(inputs.input_ids, max_length=512)
|
47 |
+
response = guard_tokenizer.decode(outputs[0], skip_special_tokens=True)
|
48 |
+
|
49 |
+
if "flagged" in response.lower():
|
50 |
+
return "⚠️ Content flagged by Llama Guard. Please modify your input."
|
51 |
+
return None # Safe input, proceed normally
|
52 |
+
|
53 |
+
# Function: Generate AI responses (Project Analysis, Code, or Risks)
|
54 |
+
def generate_response(prompt_type, **kwargs):
|
55 |
+
prompt = PROMPTS[prompt_type].format(**kwargs)
|
56 |
+
|
57 |
+
moderation_warning = moderate_input(prompt)
|
58 |
+
if moderation_warning:
|
59 |
+
return moderation_warning # Stop processing if flagged
|
60 |
+
|
61 |
+
inputs = tokenizer(prompt, return_tensors="pt", max_length=2048, truncation=True)
|
62 |
+
|
63 |
+
outputs = model.generate(
|
64 |
+
inputs.input_ids,
|
65 |
+
max_length=2048,
|
66 |
+
temperature=0.7 if prompt_type == "project_analysis" else 0.5,
|
67 |
+
top_p=0.9
|
68 |
+
)
|
69 |
+
|
70 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
71 |
+
|
72 |
+
# Function: Analyze project
|
73 |
+
def analyze_project(project_desc):
|
74 |
+
return generate_response("project_analysis", project_description=project_desc)
|
75 |
+
|
76 |
+
# Function: Generate code
|
77 |
+
def generate_code(feature_desc, lang="Python", standards="PEP8"):
|
78 |
+
return generate_response("code_generation", feature_description=feature_desc, programming_language=lang, coding_standards=standards)
|
79 |
+
|
80 |
+
# Function: Predict risks
|
81 |
+
def predict_risks(project_data):
|
82 |
+
risks = generate_response("risk_analysis", project_data=project_data)
|
83 |
+
try:
|
84 |
+
return json.loads(risks) # Convert to structured JSON if valid
|
85 |
+
except json.JSONDecodeError:
|
86 |
+
return {"error": "Invalid JSON response. Please refine your input."}
|
87 |
+
|
88 |
+
# Gradio UI
|
89 |
+
def create_gradio_interface():
|
90 |
+
with gr.Blocks(title="AI Project Manager", theme=gr.themes.Soft()) as demo:
|
91 |
+
gr.Markdown("# 🚀 AI-Powered Project Manager & Code Assistant")
|
92 |
+
|
93 |
+
# Project Analysis Tab
|
94 |
+
with gr.Tab("Project Setup"):
|
95 |
+
project_input = gr.Textbox(label="Project Description", lines=5, placeholder="Describe your project...")
|
96 |
+
project_output = gr.JSON(label="Project Analysis")
|
97 |
+
analyze_btn = gr.Button("Analyze Project")
|
98 |
+
analyze_btn.click(analyze_project, inputs=project_input, outputs=project_output)
|
99 |
+
|
100 |
+
# Code Generation Tab
|
101 |
+
with gr.Tab("Code Assistant"):
|
102 |
+
code_input = gr.Textbox(label="Feature Description", lines=3)
|
103 |
+
lang_select = gr.Dropdown(["Python", "JavaScript", "Java", "C++"], label="Language", value="Python")
|
104 |
+
standards_select = gr.Dropdown(["PEP8", "Google", "Airbnb"], label="Coding Standard", value="PEP8")
|
105 |
+
code_output = gr.Code(label="Generated Code")
|
106 |
+
code_btn = gr.Button("Generate Code")
|
107 |
+
code_btn.click(generate_code, inputs=[code_input, lang_select, standards_select], outputs=code_output)
|
108 |
+
|
109 |
+
# Risk Analysis Tab
|
110 |
+
with gr.Tab("Risk Analysis"):
|
111 |
+
risk_input = gr.Textbox(label="Project Plan", lines=5)
|
112 |
+
risk_output = gr.JSON(label="Risk Predictions")
|
113 |
+
risk_btn = gr.Button("Predict Risks")
|
114 |
+
risk_btn.click(predict_risks, inputs=risk_input, outputs=risk_output)
|
115 |
+
|
116 |
+
# Real-time Chatbot for Collaboration
|
117 |
+
with gr.Tab("Live Collaboration"):
|
118 |
+
gr.Markdown("## Real-time Project Collaboration")
|
119 |
+
chat = gr.Chatbot(height=400)
|
120 |
+
msg = gr.Textbox(label="Chat with AI PM")
|
121 |
+
clear = gr.Button("Clear Chat")
|
122 |
+
|
123 |
+
def respond(message, chat_history):
|
124 |
+
moderation_warning = moderate_input(message)
|
125 |
+
if moderation_warning:
|
126 |
+
chat_history.append((message, moderation_warning))
|
127 |
+
return "", chat_history
|
128 |
+
|
129 |
+
prompt = f"""Project Management Chat:
|
130 |
+
Context: {message}
|
131 |
+
Chat History: {chat_history}
|
132 |
+
User: {message}
|
133 |
+
AI:"""
|
134 |
+
|
135 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
136 |
+
outputs = model.generate(inputs.input_ids, max_length=2048)
|
137 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
138 |
+
chat_history.append((message, response))
|
139 |
+
return "", chat_history
|
140 |
+
|
141 |
+
msg.submit(respond, [msg, chat], [msg, chat])
|
142 |
+
clear.click(lambda: None, None, chat, queue=False)
|
143 |
+
|
144 |
+
return demo
|
145 |
+
|
146 |
+
# Run Gradio App
|
147 |
+
if __name__ == "__main__":
|
148 |
+
interface = create_gradio_interface()
|
149 |
+
interface.launch(share=True)
|