Update app.py
#2
by
testqservicesitsolutions
- opened
app.py
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
import os
|
2 |
import gradio as gr
|
|
|
3 |
from llama_index.core import SimpleDirectoryReader, VectorStoreIndex
|
4 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
5 |
from llama_index.llms.llama_cpp import LlamaCPP
|
@@ -10,17 +11,11 @@ from llama_index.llms.llama_cpp.llama_utils import (
|
|
10 |
|
11 |
model_url = 'https://huggingface.co/bartowski/Llama-3.2-3B-Instruct-GGUF/resolve/main/Llama-3.2-3B-Instruct-Q4_K_M.gguf'
|
12 |
llm = LlamaCPP(
|
13 |
-
# You can pass in the URL to a GGML model to download it automatically
|
14 |
model_url=model_url,
|
15 |
temperature=0.1,
|
16 |
max_new_tokens=256,
|
17 |
context_window=2048,
|
18 |
-
# kwargs to pass to __call__()
|
19 |
-
generate_kwargs={},
|
20 |
-
# kwargs to pass to __init__()
|
21 |
-
# set to at least 1 to use GPU
|
22 |
model_kwargs={"n_gpu_layers": 1},
|
23 |
-
# transform inputs into Llama2 format
|
24 |
messages_to_prompt=messages_to_prompt,
|
25 |
completion_to_prompt=completion_to_prompt,
|
26 |
verbose=True,
|
@@ -29,13 +24,20 @@ llm = LlamaCPP(
|
|
29 |
embeddings = HuggingFaceEmbeddings(model_name="BAAI/bge-small-en-v1.5")
|
30 |
|
31 |
def initialize_index():
|
32 |
-
"""Initialize the vector store index from
|
33 |
-
# Load
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
documents =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
|
40 |
# Create index
|
41 |
index = VectorStoreIndex.from_documents(
|
@@ -53,25 +55,22 @@ def process_query(
|
|
53 |
message: str,
|
54 |
history: list[tuple[str, str]],
|
55 |
) -> str:
|
56 |
-
"""Process a query using the RAG system"""
|
57 |
try:
|
58 |
# Get response from the query engine
|
59 |
response = query_engine.query(
|
60 |
message,
|
61 |
-
#streaming=True
|
62 |
)
|
63 |
-
return
|
64 |
except Exception as e:
|
65 |
return f"Error processing query: {str(e)}"
|
66 |
|
67 |
-
#
|
68 |
-
|
69 |
-
process_query,
|
70 |
-
|
71 |
-
|
72 |
-
#undo_btn="Delete Previous",
|
73 |
-
#clear_btn="Clear",
|
74 |
)
|
75 |
|
76 |
if __name__ == "__main__":
|
77 |
-
|
|
|
1 |
import os
|
2 |
import gradio as gr
|
3 |
+
import json
|
4 |
from llama_index.core import SimpleDirectoryReader, VectorStoreIndex
|
5 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
6 |
from llama_index.llms.llama_cpp import LlamaCPP
|
|
|
11 |
|
12 |
model_url = 'https://huggingface.co/bartowski/Llama-3.2-3B-Instruct-GGUF/resolve/main/Llama-3.2-3B-Instruct-Q4_K_M.gguf'
|
13 |
llm = LlamaCPP(
|
|
|
14 |
model_url=model_url,
|
15 |
temperature=0.1,
|
16 |
max_new_tokens=256,
|
17 |
context_window=2048,
|
|
|
|
|
|
|
|
|
18 |
model_kwargs={"n_gpu_layers": 1},
|
|
|
19 |
messages_to_prompt=messages_to_prompt,
|
20 |
completion_to_prompt=completion_to_prompt,
|
21 |
verbose=True,
|
|
|
24 |
embeddings = HuggingFaceEmbeddings(model_name="BAAI/bge-small-en-v1.5")
|
25 |
|
26 |
def initialize_index():
|
27 |
+
"""Initialize the vector store index from JSON data."""
|
28 |
+
# Load JSON data
|
29 |
+
with open("dummy_users_with_tasks.json", "r") as file:
|
30 |
+
json_data = json.load(file)
|
31 |
+
|
32 |
+
# Convert JSON data to plain text for embedding
|
33 |
+
documents = []
|
34 |
+
for user in json_data:
|
35 |
+
tasks_summary = "\n".join(
|
36 |
+
[f"Task: {task['title']} | Start: {task['start_date']} | Due: {task['due_date']}"
|
37 |
+
for task in user["tasks"]]
|
38 |
+
)
|
39 |
+
doc_text = f"User: {user['name']} | Email: {user['email']}\nTasks:\n{tasks_summary}"
|
40 |
+
documents.append(doc_text)
|
41 |
|
42 |
# Create index
|
43 |
index = VectorStoreIndex.from_documents(
|
|
|
55 |
message: str,
|
56 |
history: list[tuple[str, str]],
|
57 |
) -> str:
|
58 |
+
"""Process a query using the RAG system."""
|
59 |
try:
|
60 |
# Get response from the query engine
|
61 |
response = query_engine.query(
|
62 |
message,
|
|
|
63 |
)
|
64 |
+
return response
|
65 |
except Exception as e:
|
66 |
return f"Error processing query: {str(e)}"
|
67 |
|
68 |
+
# Gradio interface (if needed)
|
69 |
+
interface = gr.Interface(
|
70 |
+
fn=process_query,
|
71 |
+
inputs=["text", "state"],
|
72 |
+
outputs="text",
|
|
|
|
|
73 |
)
|
74 |
|
75 |
if __name__ == "__main__":
|
76 |
+
interface.launch()
|