ccm commited on
Commit
1d48be6
·
verified ·
1 Parent(s): 306420e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -16
app.py CHANGED
@@ -16,12 +16,12 @@ GREETING = (
16
 
17
  # Constants
18
  EMBEDDING_MODEL_NAME = "all-MiniLM-L12-v2"
19
- LLM_MODEL_NAME = "Qwen/Qwen2.5-7B-Instruct"
20
  PUBLICATIONS_TO_RETRIEVE = 10
21
 
22
 
23
  def embedding(
24
- device: str = "cuda", normalize_embeddings: bool = False
25
  ) -> langchain_huggingface.HuggingFaceEmbeddings:
26
  """Loads embedding model with specified device and normalization."""
27
  return langchain_huggingface.HuggingFaceEmbeddings(
@@ -33,15 +33,11 @@ def embedding(
33
 
34
  def load_publication_vectorstore() -> langchain_community.vectorstores.FAISS:
35
  """Load the publication vectorstore safely."""
36
- try:
37
- return langchain_community.vectorstores.FAISS.load_local(
38
- folder_path="publication_vectorstore",
39
- embeddings=embedding(),
40
- allow_dangerous_deserialization=True,
41
- )
42
- except Exception as e:
43
- print(f"Error loading vectorstore: {e}")
44
- return None
45
 
46
 
47
  # Load vectorstore and models
@@ -60,9 +56,9 @@ def preprocess(query: str, k: int) -> str:
60
  "You are an AI assistant who enjoys helping users learn about research. "
61
  "Answer the following question on additive manufacturing research using the RESEARCH_EXCERPTS. "
62
  "Provide a concise ANSWER based on these excerpts. Avoid listing references.\n\n"
63
- "===== RESEARCH_EXCERPTS =====:\n{research_excerpts}\n\n"
64
- "===== USER_QUERY =====:\n{query}\n\n"
65
- "===== ANSWER =====:\n"
66
  )
67
 
68
  prompt = prompt_template.format(
@@ -74,7 +70,7 @@ def preprocess(query: str, k: int) -> str:
74
 
75
 
76
  @spaces.GPU
77
- def reply(message: str, history: list[str]) -> str:
78
  """
79
  Generates a response to the user’s message.
80
  """
@@ -83,7 +79,7 @@ def reply(message: str, history: list[str]) -> str:
83
  pipe = transformers.pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")
84
 
85
  message = preprocess(message, PUBLICATIONS_TO_RETRIEVE)
86
- return pipe(message, max_length=512)[0]["generated_text"]
87
 
88
 
89
  # Example Queries for Interface
 
16
 
17
  # Constants
18
  EMBEDDING_MODEL_NAME = "all-MiniLM-L12-v2"
19
+ LLM_MODEL_NAME = "Qwen/Qwen2.5-0.5B-Instruct"
20
  PUBLICATIONS_TO_RETRIEVE = 10
21
 
22
 
23
  def embedding(
24
+ device: str = "mps", normalize_embeddings: bool = False
25
  ) -> langchain_huggingface.HuggingFaceEmbeddings:
26
  """Loads embedding model with specified device and normalization."""
27
  return langchain_huggingface.HuggingFaceEmbeddings(
 
33
 
34
  def load_publication_vectorstore() -> langchain_community.vectorstores.FAISS:
35
  """Load the publication vectorstore safely."""
36
+ return langchain_community.vectorstores.FAISS.load_local(
37
+ folder_path="publication_vectorstore",
38
+ embeddings=embedding(),
39
+ allow_dangerous_deserialization=True,
40
+ )
 
 
 
 
41
 
42
 
43
  # Load vectorstore and models
 
56
  "You are an AI assistant who enjoys helping users learn about research. "
57
  "Answer the following question on additive manufacturing research using the RESEARCH_EXCERPTS. "
58
  "Provide a concise ANSWER based on these excerpts. Avoid listing references.\n\n"
59
+ "===== RESEARCH_EXCERPTS =====\n{research_excerpts}\n\n"
60
+ "===== USER_QUERY =====\n{query}\n\n"
61
+ "===== ANSWER =====\n"
62
  )
63
 
64
  prompt = prompt_template.format(
 
70
 
71
 
72
  @spaces.GPU
73
+ def reply(message: str) -> str:
74
  """
75
  Generates a response to the user’s message.
76
  """
 
79
  pipe = transformers.pipeline("text-generation", model="Qwen/Qwen2.5-7B-Instruct")
80
 
81
  message = preprocess(message, PUBLICATIONS_TO_RETRIEVE)
82
+ return pipe(message, max_new_tokens=512, device="mps")[0]["generated_text"]
83
 
84
 
85
  # Example Queries for Interface