Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -10,39 +10,61 @@ print(f"Using device: {device}")
|
|
10 |
|
11 |
class UnifiedAssistant:
|
12 |
def __init__(self):
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
|
|
|
|
|
|
|
|
32 |
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
@spaces.GPU
|
44 |
def process_code_query(self, query):
|
45 |
try:
|
|
|
|
|
|
|
46 |
inputs = self.code_tokenizer(query, return_tensors="pt").to(self.code_model.device)
|
47 |
outputs = self.code_model.generate(
|
48 |
**inputs,
|
@@ -53,11 +75,14 @@ class UnifiedAssistant:
|
|
53 |
)
|
54 |
return self.code_tokenizer.decode(outputs[0], skip_special_tokens=True)
|
55 |
except Exception as e:
|
|
|
56 |
return f"Error processing code query: {str(e)}"
|
57 |
|
58 |
@spaces.GPU
|
59 |
def process_docs_query(self, query, doc_file):
|
60 |
try:
|
|
|
|
|
61 |
if doc_file is None:
|
62 |
return "Please upload a documentation file."
|
63 |
|
@@ -73,11 +98,14 @@ class UnifiedAssistant:
|
|
73 |
)
|
74 |
return self.docs_tokenizer.decode(outputs[0], skip_special_tokens=True)
|
75 |
except Exception as e:
|
|
|
76 |
return f"Error processing documentation query: {str(e)}"
|
77 |
|
78 |
@spaces.GPU
|
79 |
def process_pdf_query(self, query, pdf_file):
|
80 |
try:
|
|
|
|
|
81 |
if pdf_file is None:
|
82 |
return "Please upload a PDF file."
|
83 |
|
@@ -93,22 +121,31 @@ class UnifiedAssistant:
|
|
93 |
)
|
94 |
return self.pdf_tokenizer.decode(outputs[0], skip_special_tokens=True)
|
95 |
except Exception as e:
|
|
|
96 |
return f"Error processing PDF query: {str(e)}"
|
97 |
|
98 |
def _read_file_content(self, file):
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
|
|
|
|
|
|
|
|
105 |
|
106 |
def _extract_pdf_text(self, pdf_file):
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
|
|
|
|
|
|
|
|
112 |
|
113 |
# Custom CSS for better UI
|
114 |
css = """
|
|
|
10 |
|
11 |
class UnifiedAssistant:
|
12 |
def __init__(self):
|
13 |
+
try:
|
14 |
+
# Initialize Code Assistant (Qwen)
|
15 |
+
print("Loading Code Assistant Model...")
|
16 |
+
self.code_model_name = "Qwen/Qwen2.5-Coder-32B-Instruct"
|
17 |
+
self.code_tokenizer = AutoTokenizer.from_pretrained(
|
18 |
+
self.code_model_name,
|
19 |
+
trust_remote_code=True
|
20 |
+
)
|
21 |
+
self.code_model = AutoModelForCausalLM.from_pretrained(
|
22 |
+
self.code_model_name,
|
23 |
+
torch_dtype=torch.bfloat16,
|
24 |
+
device_map="auto",
|
25 |
+
trust_remote_code=True
|
26 |
+
)
|
27 |
|
28 |
+
# Initialize Docs Assistant (DocGPT)
|
29 |
+
print("Loading Documentation Assistant Model...")
|
30 |
+
self.docs_model_name = "Arc53/docsgpt-40b-falcon"
|
31 |
+
self.docs_tokenizer = AutoTokenizer.from_pretrained(
|
32 |
+
self.docs_model_name,
|
33 |
+
trust_remote_code=True
|
34 |
+
)
|
35 |
+
self.docs_model = AutoModelForCausalLM.from_pretrained(
|
36 |
+
self.docs_model_name,
|
37 |
+
torch_dtype=torch.bfloat16,
|
38 |
+
device_map="auto",
|
39 |
+
trust_remote_code=True
|
40 |
+
)
|
41 |
|
42 |
+
# Initialize PDF Assistant (Llama)
|
43 |
+
print("Loading PDF Assistant Model...")
|
44 |
+
self.pdf_model_name = "meta-llama/Llama-3.3-70B-Instruct"
|
45 |
+
self.pdf_tokenizer = AutoTokenizer.from_pretrained(
|
46 |
+
self.pdf_model_name,
|
47 |
+
trust_remote_code=True
|
48 |
+
)
|
49 |
+
self.pdf_model = AutoModelForCausalLM.from_pretrained(
|
50 |
+
self.pdf_model_name,
|
51 |
+
torch_dtype=torch.bfloat16,
|
52 |
+
device_map="auto",
|
53 |
+
trust_remote_code=True
|
54 |
+
)
|
55 |
+
|
56 |
+
print("All models loaded successfully!")
|
57 |
+
|
58 |
+
except Exception as e:
|
59 |
+
print(f"Error initializing models: {str(e)}")
|
60 |
+
raise RuntimeError(f"Failed to initialize one or more models: {str(e)}")
|
61 |
|
62 |
@spaces.GPU
|
63 |
def process_code_query(self, query):
|
64 |
try:
|
65 |
+
if not query.strip():
|
66 |
+
return "Please enter a coding question."
|
67 |
+
|
68 |
inputs = self.code_tokenizer(query, return_tensors="pt").to(self.code_model.device)
|
69 |
outputs = self.code_model.generate(
|
70 |
**inputs,
|
|
|
75 |
)
|
76 |
return self.code_tokenizer.decode(outputs[0], skip_special_tokens=True)
|
77 |
except Exception as e:
|
78 |
+
print(f"Code query error: {str(e)}")
|
79 |
return f"Error processing code query: {str(e)}"
|
80 |
|
81 |
@spaces.GPU
|
82 |
def process_docs_query(self, query, doc_file):
|
83 |
try:
|
84 |
+
if not query.strip():
|
85 |
+
return "Please enter a documentation query."
|
86 |
if doc_file is None:
|
87 |
return "Please upload a documentation file."
|
88 |
|
|
|
98 |
)
|
99 |
return self.docs_tokenizer.decode(outputs[0], skip_special_tokens=True)
|
100 |
except Exception as e:
|
101 |
+
print(f"Documentation query error: {str(e)}")
|
102 |
return f"Error processing documentation query: {str(e)}"
|
103 |
|
104 |
@spaces.GPU
|
105 |
def process_pdf_query(self, query, pdf_file):
|
106 |
try:
|
107 |
+
if not query.strip():
|
108 |
+
return "Please enter a question about the PDF."
|
109 |
if pdf_file is None:
|
110 |
return "Please upload a PDF file."
|
111 |
|
|
|
121 |
)
|
122 |
return self.pdf_tokenizer.decode(outputs[0], skip_special_tokens=True)
|
123 |
except Exception as e:
|
124 |
+
print(f"PDF query error: {str(e)}")
|
125 |
return f"Error processing PDF query: {str(e)}"
|
126 |
|
127 |
def _read_file_content(self, file):
|
128 |
+
try:
|
129 |
+
content = ""
|
130 |
+
if file.name.endswith('.pdf'):
|
131 |
+
content = self._extract_pdf_text(file)
|
132 |
+
else:
|
133 |
+
content = file.read().decode('utf-8')
|
134 |
+
return content
|
135 |
+
except Exception as e:
|
136 |
+
print(f"File reading error: {str(e)}")
|
137 |
+
raise
|
138 |
|
139 |
def _extract_pdf_text(self, pdf_file):
|
140 |
+
try:
|
141 |
+
reader = PdfReader(pdf_file)
|
142 |
+
text = ""
|
143 |
+
for page in reader.pages:
|
144 |
+
text += page.extract_text() + "\n"
|
145 |
+
return text
|
146 |
+
except Exception as e:
|
147 |
+
print(f"PDF extraction error: {str(e)}")
|
148 |
+
raise
|
149 |
|
150 |
# Custom CSS for better UI
|
151 |
css = """
|