Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
# Multi-agent UI generator with
|
2 |
|
3 |
import streamlit as st
|
4 |
import time
|
@@ -6,7 +6,7 @@ import base64
|
|
6 |
from typing import Dict, List, TypedDict
|
7 |
from langgraph.graph import StateGraph, END
|
8 |
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
9 |
-
from peft import PeftModel
|
10 |
import torch
|
11 |
import os
|
12 |
|
@@ -14,7 +14,6 @@ st.set_page_config(page_title="Multi-Agent Collaboration", layout="wide")
|
|
14 |
|
15 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
16 |
|
17 |
-
# Agent model loading config
|
18 |
AGENT_MODEL_CONFIG = {
|
19 |
"product_manager": {
|
20 |
"base": "google/gemma-1.1-7b-it",
|
@@ -29,7 +28,7 @@ AGENT_MODEL_CONFIG = {
|
|
29 |
"adapter": "spandana30/software-architect-cohere"
|
30 |
},
|
31 |
"software_engineer": {
|
32 |
-
"base": "codellama/
|
33 |
"adapter": "spandana30/software-engineer-codellama"
|
34 |
},
|
35 |
"qa": {
|
@@ -40,28 +39,20 @@ AGENT_MODEL_CONFIG = {
|
|
40 |
|
41 |
@st.cache_resource
|
42 |
|
43 |
-
def
|
|
|
44 |
try:
|
45 |
-
st.write(f"π Loading: {
|
46 |
-
st.write(f"π Using token: {'Yes' if HF_TOKEN else 'No'}")
|
47 |
-
|
48 |
base_model = AutoModelForCausalLM.from_pretrained(
|
49 |
-
|
50 |
)
|
51 |
-
model = PeftModel.from_pretrained(base_model,
|
52 |
-
tokenizer = AutoTokenizer.from_pretrained(
|
53 |
-
|
54 |
return pipeline("text-generation", model=model, tokenizer=tokenizer, max_new_tokens=1024)
|
55 |
-
|
56 |
except Exception as e:
|
57 |
-
st.error(f"β
|
58 |
raise
|
59 |
|
60 |
-
AGENT_PIPELINES = {
|
61 |
-
role: load_agent_model(cfg["base"], cfg["adapter"])
|
62 |
-
for role, cfg in AGENT_MODEL_CONFIG.items()
|
63 |
-
}
|
64 |
-
|
65 |
class AgentState(TypedDict):
|
66 |
messages: List[Dict[str, str]]
|
67 |
user_request: str
|
@@ -75,8 +66,8 @@ class AgentState(TypedDict):
|
|
75 |
timings: Dict[str, float]
|
76 |
|
77 |
def run_pipeline(role: str, prompt: str):
|
78 |
-
|
79 |
-
return
|
80 |
|
81 |
PROMPTS = {
|
82 |
"product_manager": """You're a Product Manager. Refine and clarify this request:
|
@@ -189,4 +180,4 @@ def main():
|
|
189 |
st.write(f"π§© {stage.replace('_', ' ').title()} Time: {final_state['timings'].get(stage, 0):.2f}s")
|
190 |
|
191 |
if __name__ == "__main__":
|
192 |
-
main()
|
|
|
1 |
+
# Multi-agent UI generator with lazy model loading to reduce memory usage
|
2 |
|
3 |
import streamlit as st
|
4 |
import time
|
|
|
6 |
from typing import Dict, List, TypedDict
|
7 |
from langgraph.graph import StateGraph, END
|
8 |
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
9 |
+
from peft import PeftModel
|
10 |
import torch
|
11 |
import os
|
12 |
|
|
|
14 |
|
15 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
16 |
|
|
|
17 |
AGENT_MODEL_CONFIG = {
|
18 |
"product_manager": {
|
19 |
"base": "google/gemma-1.1-7b-it",
|
|
|
28 |
"adapter": "spandana30/software-architect-cohere"
|
29 |
},
|
30 |
"software_engineer": {
|
31 |
+
"base": "codellama/CodeLLaMA-7b-Instruct-hf",
|
32 |
"adapter": "spandana30/software-engineer-codellama"
|
33 |
},
|
34 |
"qa": {
|
|
|
39 |
|
40 |
@st.cache_resource
|
41 |
|
42 |
+
def get_text_pipeline(role: str):
|
43 |
+
cfg = AGENT_MODEL_CONFIG[role]
|
44 |
try:
|
45 |
+
st.write(f"π Loading model for {role}: {cfg['base']} + {cfg['adapter']}")
|
|
|
|
|
46 |
base_model = AutoModelForCausalLM.from_pretrained(
|
47 |
+
cfg["base"], torch_dtype=torch.float16, device_map="auto", token=HF_TOKEN
|
48 |
)
|
49 |
+
model = PeftModel.from_pretrained(base_model, cfg["adapter"], token=HF_TOKEN)
|
50 |
+
tokenizer = AutoTokenizer.from_pretrained(cfg["adapter"], token=HF_TOKEN)
|
|
|
51 |
return pipeline("text-generation", model=model, tokenizer=tokenizer, max_new_tokens=1024)
|
|
|
52 |
except Exception as e:
|
53 |
+
st.error(f"β Failed to load model for {role}\nError: {e}")
|
54 |
raise
|
55 |
|
|
|
|
|
|
|
|
|
|
|
56 |
class AgentState(TypedDict):
|
57 |
messages: List[Dict[str, str]]
|
58 |
user_request: str
|
|
|
66 |
timings: Dict[str, float]
|
67 |
|
68 |
def run_pipeline(role: str, prompt: str):
|
69 |
+
pipe = get_text_pipeline(role)
|
70 |
+
return pipe(prompt, do_sample=False)[0]["generated_text"].strip()
|
71 |
|
72 |
PROMPTS = {
|
73 |
"product_manager": """You're a Product Manager. Refine and clarify this request:
|
|
|
180 |
st.write(f"π§© {stage.replace('_', ' ').title()} Time: {final_state['timings'].get(stage, 0):.2f}s")
|
181 |
|
182 |
if __name__ == "__main__":
|
183 |
+
main()
|