Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -21,12 +21,21 @@ class RAGInterface:
|
|
| 21 |
embedding_function=self.embeddings
|
| 22 |
)
|
| 23 |
|
| 24 |
-
#
|
| 25 |
-
self.
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
# Define RAG prompt template
|
| 32 |
self.template = """Answer the question based only on the following context:
|
|
@@ -43,7 +52,20 @@ class RAGInterface:
|
|
| 43 |
"""
|
| 44 |
self.prompt = PromptTemplate.from_template(self.template)
|
| 45 |
|
| 46 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
# Build messages list
|
| 48 |
messages = [{"role": "system", "content": system_message}]
|
| 49 |
for user_msg, assistant_msg in history:
|
|
@@ -161,14 +183,12 @@ class RAGInterface:
|
|
| 161 |
label="System Message",
|
| 162 |
elem_classes="control-panel"
|
| 163 |
),
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
# elem_classes="control-panel"
|
| 171 |
-
# ),
|
| 172 |
gr.Slider(
|
| 173 |
minimum=0.1,
|
| 174 |
maximum=1.0,
|
|
@@ -180,11 +200,6 @@ class RAGInterface:
|
|
| 180 |
],
|
| 181 |
title="", # Title is handled in custom HTML
|
| 182 |
description="Ask questions about Computers and get AI-powered answers.",
|
| 183 |
-
# examples=[
|
| 184 |
-
# "What is a Computer?",
|
| 185 |
-
# "How does machine learning work?",
|
| 186 |
-
# "Explain artificial intelligence.",
|
| 187 |
-
# ],
|
| 188 |
theme=gr.themes.Default(),
|
| 189 |
)
|
| 190 |
|
|
|
|
| 21 |
embedding_function=self.embeddings
|
| 22 |
)
|
| 23 |
|
| 24 |
+
# Model configurations
|
| 25 |
+
self.model_configs = {
|
| 26 |
+
"Llama 3.2 3B (Fast, Less Accurate)": {
|
| 27 |
+
"repo_id": "bartowski/Llama-3.2-3B-Instruct-GGUF",
|
| 28 |
+
"filename": "Llama-3.2-3B-Instruct-Q6_K.gguf",
|
| 29 |
+
},
|
| 30 |
+
"Llama 3.1 8B (Slower, More Accurate)": {
|
| 31 |
+
"repo_id": "bartowski/Meta-Llama-3.1-8B-Instruct-GGUF",
|
| 32 |
+
"filename": "Meta-Llama-3.1-8B-Instruct-Q4_K_M.gguf",
|
| 33 |
+
}
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
# Initialize with default model
|
| 37 |
+
self.current_model = "Llama 3.1 8B (Slower, More Accurate)"
|
| 38 |
+
self.load_model(self.current_model)
|
| 39 |
|
| 40 |
# Define RAG prompt template
|
| 41 |
self.template = """Answer the question based only on the following context:
|
|
|
|
| 52 |
"""
|
| 53 |
self.prompt = PromptTemplate.from_template(self.template)
|
| 54 |
|
| 55 |
+
def load_model(self, model_name):
|
| 56 |
+
config = self.model_configs[model_name]
|
| 57 |
+
self.llm = Llama.from_pretrained(
|
| 58 |
+
repo_id=config["repo_id"],
|
| 59 |
+
filename=config["filename"],
|
| 60 |
+
n_ctx=2048
|
| 61 |
+
)
|
| 62 |
+
self.current_model = model_name
|
| 63 |
+
|
| 64 |
+
def respond(self, message, history, system_message, model_choice, temperature, max_tokens=2048):
|
| 65 |
+
# Load new model if different from current
|
| 66 |
+
if model_choice != self.current_model:
|
| 67 |
+
self.load_model(model_choice)
|
| 68 |
+
|
| 69 |
# Build messages list
|
| 70 |
messages = [{"role": "system", "content": system_message}]
|
| 71 |
for user_msg, assistant_msg in history:
|
|
|
|
| 183 |
label="System Message",
|
| 184 |
elem_classes="control-panel"
|
| 185 |
),
|
| 186 |
+
gr.Dropdown(
|
| 187 |
+
choices=list(self.model_configs.keys()),
|
| 188 |
+
value=self.current_model,
|
| 189 |
+
label="Select Model",
|
| 190 |
+
elem_classes="control-panel"
|
| 191 |
+
),
|
|
|
|
|
|
|
| 192 |
gr.Slider(
|
| 193 |
minimum=0.1,
|
| 194 |
maximum=1.0,
|
|
|
|
| 200 |
],
|
| 201 |
title="", # Title is handled in custom HTML
|
| 202 |
description="Ask questions about Computers and get AI-powered answers.",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 203 |
theme=gr.themes.Default(),
|
| 204 |
)
|
| 205 |
|