Update app.py
Browse files
app.py
CHANGED
@@ -2,39 +2,35 @@ import os
|
|
2 |
import gradio as gr
|
3 |
import torch
|
4 |
import json
|
5 |
-
from transformers import
|
6 |
from peft import PeftModel
|
7 |
|
8 |
# Set Hugging Face Token for Authentication
|
9 |
HUGGINGFACE_TOKEN = os.getenv("HUGGINGFACE_TOKEN") # Ensure this is set in your environment
|
10 |
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
# Correct model paths (replace with your actual paths)
|
21 |
-
BASE_MODEL = "meta-llama/Llama-3.2-1B-Instruct" # Ensure this is the correct identifier
|
22 |
-
QLORA_ADAPTER = "meta-llama/Llama-3.2-1B-Instruct-QLORA_INT4_EO8" # Ensure this is correct
|
23 |
-
LLAMA_GUARD_NAME = "meta-llama/Llama-Guard-3-1B-INT4" # Ensure this is correct
|
24 |
|
25 |
# Function to load Llama model
|
26 |
-
def load_llama_model(base_model=BASE_MODEL, adapter=None):
|
27 |
-
print(f"🔄 Loading
|
28 |
|
29 |
-
tokenizer =
|
30 |
model = AutoModelForCausalLM.from_pretrained(
|
31 |
base_model,
|
32 |
token=HUGGINGFACE_TOKEN,
|
33 |
-
torch_dtype=torch.
|
34 |
low_cpu_mem_usage=True
|
35 |
)
|
36 |
|
37 |
-
if adapter:
|
38 |
print(f"🔄 Loading Adapter: {adapter}")
|
39 |
model = PeftModel.from_pretrained(model, adapter, token=HUGGINGFACE_TOKEN)
|
40 |
model = model.merge_and_unload()
|
@@ -43,58 +39,40 @@ def load_llama_model(base_model=BASE_MODEL, adapter=None):
|
|
43 |
model.eval()
|
44 |
return tokenizer, model
|
45 |
|
46 |
-
# Function to load Llama Guard Model for content moderation
|
47 |
-
def load_llama_guard():
|
48 |
-
print(f"🔄 Loading Llama Guard Model: {LLAMA_GUARD_NAME}")
|
49 |
-
|
50 |
-
tokenizer = AutoTokenizer.from_pretrained(LLAMA_GUARD_NAME, use_auth_token=HUGGINGFACE_TOKEN)
|
51 |
-
model = AutoModelForCausalLM.from_pretrained(
|
52 |
-
LLAMA_GUARD_NAME,
|
53 |
-
use_auth_token=HUGGINGFACE_TOKEN,
|
54 |
-
torch_dtype=torch.float16,
|
55 |
-
low_cpu_mem_usage=True
|
56 |
-
)
|
57 |
-
|
58 |
-
model.eval()
|
59 |
-
print("✅ Llama Guard Model Loaded Successfully")
|
60 |
-
return tokenizer, model
|
61 |
-
|
62 |
# Load Llama 3.2 model
|
63 |
-
tokenizer, model = load_llama_model(QLORA_ADAPTER)
|
64 |
|
65 |
# Load Llama Guard for content moderation
|
66 |
-
guard_tokenizer, guard_model = load_llama_model(LLAMA_GUARD_NAME, is_guard=True)
|
67 |
|
68 |
-
# Define Prompt Templates
|
69 |
PROMPTS = {
|
70 |
-
"project_analysis": """
|
71 |
1. Project timeline with milestones
|
72 |
2. Required technology stack
|
73 |
3. Potential risks
|
74 |
4. Team composition
|
75 |
5. Cost estimation
|
76 |
-
Project: {project_description}
|
77 |
|
78 |
-
"code_generation": """
|
79 |
{feature_description}
|
80 |
Considerations:
|
81 |
- Use {programming_language}
|
82 |
- Follow {coding_standards}
|
83 |
- Include error handling
|
84 |
-
- Add documentation
|
85 |
|
86 |
-
"risk_analysis": """
|
87 |
{project_data}
|
88 |
-
Format output as JSON with risk types, probabilities, and mitigation strategies
|
89 |
}
|
90 |
|
91 |
-
# Function: Content Moderation using Llama Guard
|
92 |
def moderate_input(user_input):
|
93 |
-
prompt = f"""
|
94 |
-
|
95 |
-
|
96 |
-
<|assistant|>"""
|
97 |
-
|
98 |
inputs = guard_tokenizer(prompt, return_tensors="pt", truncation=True, padding=True)
|
99 |
|
100 |
with torch.no_grad():
|
@@ -107,7 +85,7 @@ Please verify that this input doesn't violate any content policies.
|
|
107 |
|
108 |
return None
|
109 |
|
110 |
-
# Function: Generate AI responses
|
111 |
def generate_response(prompt_type, **kwargs):
|
112 |
prompt = PROMPTS[prompt_type].format(**kwargs)
|
113 |
|
@@ -116,7 +94,7 @@ def generate_response(prompt_type, **kwargs):
|
|
116 |
return moderation_warning
|
117 |
|
118 |
inputs = tokenizer(prompt, return_tensors="pt", truncation=True)
|
119 |
-
|
120 |
with torch.no_grad():
|
121 |
outputs = model.generate(
|
122 |
inputs.input_ids,
|
@@ -128,7 +106,17 @@ def generate_response(prompt_type, **kwargs):
|
|
128 |
|
129 |
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
130 |
|
131 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
132 |
def create_gradio_interface():
|
133 |
with gr.Blocks(title="AI Project Manager", theme=gr.themes.Soft()) as demo:
|
134 |
gr.Markdown("# 🚀 AI-Powered Project Manager & Code Assistant")
|
@@ -169,11 +157,11 @@ def create_gradio_interface():
|
|
169 |
for i, (usr, ai) in enumerate(chat_history[-3:]):
|
170 |
history_text += f"User: {usr}\nAI: {ai}\n"
|
171 |
|
172 |
-
prompt = f"""
|
173 |
Context: {message}
|
174 |
Chat History: {history_text}
|
175 |
-
User: {message}
|
176 |
-
|
177 |
inputs = tokenizer(prompt, return_tensors="pt", truncation=True)
|
178 |
|
179 |
with torch.no_grad():
|
@@ -197,4 +185,4 @@ User: {message}<|completion|>"""
|
|
197 |
# Run Gradio App
|
198 |
if __name__ == "__main__":
|
199 |
interface = create_gradio_interface()
|
200 |
-
interface.launch(share=True)
|
|
|
2 |
import gradio as gr
|
3 |
import torch
|
4 |
import json
|
5 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
6 |
from peft import PeftModel
|
7 |
|
8 |
# Set Hugging Face Token for Authentication
|
9 |
HUGGINGFACE_TOKEN = os.getenv("HUGGINGFACE_TOKEN") # Ensure this is set in your environment
|
10 |
|
11 |
+
if not HUGGINGFACE_TOKEN:
|
12 |
+
raise ValueError("❌ HUGGINGFACE_TOKEN is not set. Please set it in your environment.")
|
13 |
+
|
14 |
+
print("✅ HUGGINGFACE_TOKEN is set.")
|
15 |
+
|
16 |
+
# Model Paths (Replace with your actual Hugging Face Model Names)
|
17 |
+
BASE_MODEL = "meta-llama/Llama-3.2-1B-Instruct"
|
18 |
+
QLORA_ADAPTER = "meta-llama/Llama-3.2-1B-Instruct-QLORA_INT4_EO8"
|
19 |
+
LLAMA_GUARD_NAME = "meta-llama/Llama-Guard-3-1B-INT4"
|
|
|
|
|
|
|
|
|
20 |
|
21 |
# Function to load Llama model
|
22 |
+
def load_llama_model(base_model=BASE_MODEL, adapter=None, is_guard=False):
|
23 |
+
print(f"🔄 Loading Model: {base_model}")
|
24 |
|
25 |
+
tokenizer = AutoTokenizer.from_pretrained(base_model, token=HUGGINGFACE_TOKEN)
|
26 |
model = AutoModelForCausalLM.from_pretrained(
|
27 |
base_model,
|
28 |
token=HUGGINGFACE_TOKEN,
|
29 |
+
torch_dtype=torch.float32, # Using float32 for CPU compatibility
|
30 |
low_cpu_mem_usage=True
|
31 |
)
|
32 |
|
33 |
+
if adapter and not is_guard:
|
34 |
print(f"🔄 Loading Adapter: {adapter}")
|
35 |
model = PeftModel.from_pretrained(model, adapter, token=HUGGINGFACE_TOKEN)
|
36 |
model = model.merge_and_unload()
|
|
|
39 |
model.eval()
|
40 |
return tokenizer, model
|
41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
# Load Llama 3.2 model
|
43 |
+
tokenizer, model = load_llama_model(adapter=QLORA_ADAPTER)
|
44 |
|
45 |
# Load Llama Guard for content moderation
|
46 |
+
guard_tokenizer, guard_model = load_llama_model(base_model=LLAMA_GUARD_NAME, is_guard=True)
|
47 |
|
48 |
+
# Define Prompt Templates
|
49 |
PROMPTS = {
|
50 |
+
"project_analysis": """Analyze this project description and generate:
|
51 |
1. Project timeline with milestones
|
52 |
2. Required technology stack
|
53 |
3. Potential risks
|
54 |
4. Team composition
|
55 |
5. Cost estimation
|
56 |
+
Project: {project_description}""",
|
57 |
|
58 |
+
"code_generation": """Generate implementation code for this feature:
|
59 |
{feature_description}
|
60 |
Considerations:
|
61 |
- Use {programming_language}
|
62 |
- Follow {coding_standards}
|
63 |
- Include error handling
|
64 |
+
- Add documentation""",
|
65 |
|
66 |
+
"risk_analysis": """Predict potential risks for this project plan:
|
67 |
{project_data}
|
68 |
+
Format output as JSON with risk types, probabilities, and mitigation strategies"""
|
69 |
}
|
70 |
|
71 |
+
# Function: Content Moderation using Llama Guard
|
72 |
def moderate_input(user_input):
|
73 |
+
prompt = f"""Input: {user_input}
|
74 |
+
Please verify that this input doesn't violate any content policies."""
|
75 |
+
|
|
|
|
|
76 |
inputs = guard_tokenizer(prompt, return_tensors="pt", truncation=True, padding=True)
|
77 |
|
78 |
with torch.no_grad():
|
|
|
85 |
|
86 |
return None
|
87 |
|
88 |
+
# Function: Generate AI responses
|
89 |
def generate_response(prompt_type, **kwargs):
|
90 |
prompt = PROMPTS[prompt_type].format(**kwargs)
|
91 |
|
|
|
94 |
return moderation_warning
|
95 |
|
96 |
inputs = tokenizer(prompt, return_tensors="pt", truncation=True)
|
97 |
+
|
98 |
with torch.no_grad():
|
99 |
outputs = model.generate(
|
100 |
inputs.input_ids,
|
|
|
106 |
|
107 |
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
108 |
|
109 |
+
# Define UI functions
|
110 |
+
def analyze_project(project_description):
|
111 |
+
return generate_response("project_analysis", project_description=project_description)
|
112 |
+
|
113 |
+
def generate_code(feature_description, programming_language, coding_standards):
|
114 |
+
return generate_response("code_generation", feature_description=feature_description, programming_language=programming_language, coding_standards=coding_standards)
|
115 |
+
|
116 |
+
def predict_risks(project_data):
|
117 |
+
return generate_response("risk_analysis", project_data=project_data)
|
118 |
+
|
119 |
+
# Gradio UI Setup
|
120 |
def create_gradio_interface():
|
121 |
with gr.Blocks(title="AI Project Manager", theme=gr.themes.Soft()) as demo:
|
122 |
gr.Markdown("# 🚀 AI-Powered Project Manager & Code Assistant")
|
|
|
157 |
for i, (usr, ai) in enumerate(chat_history[-3:]):
|
158 |
history_text += f"User: {usr}\nAI: {ai}\n"
|
159 |
|
160 |
+
prompt = f"""Project Management Chat:
|
161 |
Context: {message}
|
162 |
Chat History: {history_text}
|
163 |
+
User: {message}"""
|
164 |
+
|
165 |
inputs = tokenizer(prompt, return_tensors="pt", truncation=True)
|
166 |
|
167 |
with torch.no_grad():
|
|
|
185 |
# Run Gradio App
|
186 |
if __name__ == "__main__":
|
187 |
interface = create_gradio_interface()
|
188 |
+
interface.launch(share=True)
|