Spaces:
Runtime error
Runtime error
nananie143
commited on
Commit
·
bdc6438
1
Parent(s):
49575a4
Fixed model loading and agent initialization
Browse files
app.py
CHANGED
@@ -16,7 +16,7 @@ import networkx as nx
|
|
16 |
from langchain.prompts import PromptTemplate
|
17 |
import torch
|
18 |
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
19 |
-
from
|
20 |
from langchain.agents import initialize_agent, Tool
|
21 |
import subprocess
|
22 |
import asyncio
|
@@ -26,33 +26,19 @@ logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(
|
|
26 |
logger = logging.getLogger(__name__)
|
27 |
|
28 |
# Load the LLM and tokenizer
|
29 |
-
MODEL_NAME = "unit-mesh/autodev-coder-deepseek-6.7b-finetunes"
|
30 |
-
|
31 |
def load_model():
|
|
|
32 |
try:
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
gpu_memory = torch.cuda.get_device_properties(0).total_memory / 1024**3 # Convert to GB
|
39 |
-
if gpu_memory < 8: # If less than 8GB available
|
40 |
-
logger.warning("Limited GPU memory available. Using CPU instead.")
|
41 |
-
device = "cpu"
|
42 |
-
else:
|
43 |
-
device = "cpu"
|
44 |
-
logger.info("No GPU detected. Using CPU.")
|
45 |
|
46 |
-
model = AutoModelForCausalLM.from_pretrained(
|
47 |
-
MODEL_NAME,
|
48 |
-
torch_dtype=torch.float16 if device == "cuda" else torch.float32,
|
49 |
-
device_map="auto" if device == "cuda" else None,
|
50 |
-
low_cpu_mem_usage=True
|
51 |
-
)
|
52 |
return tokenizer, model
|
53 |
except Exception as e:
|
54 |
logger.error(f"Failed to load model: {str(e)}")
|
55 |
-
raise
|
56 |
|
57 |
# Initialize models lazily
|
58 |
tokenizer = None
|
@@ -61,44 +47,54 @@ hf_pipeline = None
|
|
61 |
llm = None
|
62 |
|
63 |
def get_llm():
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
model=
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
|
77 |
-
# Lazy initialization of agents
|
78 |
def get_agent(agent_type):
|
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 |
# Enhanced prompt templates with more specific instructions
|
104 |
ui_designer_prompt = PromptTemplate(
|
|
|
16 |
from langchain.prompts import PromptTemplate
|
17 |
import torch
|
18 |
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
19 |
+
from langchain.llms import HuggingFacePipeline
|
20 |
from langchain.agents import initialize_agent, Tool
|
21 |
import subprocess
|
22 |
import asyncio
|
|
|
26 |
logger = logging.getLogger(__name__)
|
27 |
|
28 |
# Load the LLM and tokenizer
|
|
|
|
|
29 |
def load_model():
|
30 |
+
"""Load the model and tokenizer."""
|
31 |
try:
|
32 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
33 |
+
|
34 |
+
model_name = "gpt2" # Using a smaller model for testing
|
35 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
36 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
return tokenizer, model
|
39 |
except Exception as e:
|
40 |
logger.error(f"Failed to load model: {str(e)}")
|
41 |
+
raise
|
42 |
|
43 |
# Initialize models lazily
|
44 |
tokenizer = None
|
|
|
47 |
llm = None
|
48 |
|
49 |
def get_llm():
|
50 |
+
"""Get or initialize the language model."""
|
51 |
+
global llm, tokenizer, model, hf_pipeline
|
52 |
+
|
53 |
+
try:
|
54 |
+
if llm is None:
|
55 |
+
tokenizer, model = load_model()
|
56 |
+
hf_pipeline = pipeline(
|
57 |
+
"text-generation",
|
58 |
+
model=model,
|
59 |
+
tokenizer=tokenizer,
|
60 |
+
max_length=500,
|
61 |
+
temperature=0.7,
|
62 |
+
)
|
63 |
+
llm = HuggingFacePipeline(pipeline=hf_pipeline)
|
64 |
+
return llm
|
65 |
+
except Exception as e:
|
66 |
+
logger.error(f"Failed to get LLM: {str(e)}")
|
67 |
+
raise
|
68 |
|
|
|
69 |
def get_agent(agent_type):
|
70 |
+
"""Get or initialize an agent with the specified type."""
|
71 |
+
try:
|
72 |
+
llm = get_llm()
|
73 |
+
return initialize_agent(
|
74 |
+
tools=[
|
75 |
+
Tool(
|
76 |
+
name="Code Formatter",
|
77 |
+
func=lambda x: subprocess.run(["black", "-"], input=x.encode(), capture_output=True).stdout.decode(),
|
78 |
+
description="Formats code using Black.",
|
79 |
+
),
|
80 |
+
Tool(
|
81 |
+
name="API Generator",
|
82 |
+
func=lambda x: json.dumps({"endpoints": {"example": "POST - Example endpoint."}}),
|
83 |
+
description="Generates API details from code.",
|
84 |
+
),
|
85 |
+
Tool(
|
86 |
+
name="Task Decomposer",
|
87 |
+
func=lambda x: json.dumps({"tasks": ["Design UI", "Develop Backend", "Test App", "Deploy App"]}),
|
88 |
+
description="Breaks down app requirements into smaller tasks.",
|
89 |
+
),
|
90 |
+
],
|
91 |
+
llm=llm,
|
92 |
+
agent="zero-shot-react-description",
|
93 |
+
verbose=True,
|
94 |
+
)
|
95 |
+
except Exception as e:
|
96 |
+
logger.error(f"Failed to get agent: {str(e)}")
|
97 |
+
raise
|
98 |
|
99 |
# Enhanced prompt templates with more specific instructions
|
100 |
ui_designer_prompt = PromptTemplate(
|