IamLevin40 commited on
Commit
b0d2fff
·
verified ·
1 Parent(s): 879dc70

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -11
app.py CHANGED
@@ -19,40 +19,58 @@ model_path = hf_hub_download(
19
 
20
  print("🧠 Loading model into llama.cpp engine...")
21
  # 2. Initialize llama.cpp
22
- # This is highly optimized C++ code running under the hood
23
  llm = Llama(
24
  model_path=model_path,
25
- n_ctx=4096, # Context window (how much conversation it can remember)
26
  n_threads=2, # Perfectly matches the Hugging Face Free Tier CPU limits
27
- verbose=False # Hides annoying C++ log spam
28
  )
29
  print("✅ Model loaded and ready!")
30
 
31
  def generate_answer(message: str) -> str:
 
 
32
  prompt = (
33
  "<|user|>\n"
34
  "You are ChemGist, an intelligent and conversational chemistry assistant. Answer the user's question accurately based on your training.\n\n"
35
- "Answer the user's question by weaving the following facts into a natural, flowing paragraph. "
36
- "CRITICAL: Do NOT use colons. Do NOT use bullet points. Do NOT output a list. "
37
- "Do NOT mention 'Unnamed Compound' or the 'ChEMBL database' in your response. "
38
- "Write in complete sentences as if explaining it to a friend.\n\n"
 
39
  f"Question: {message}<|end|>\n"
40
  "<|assistant|>\n"
41
  )
42
 
43
- # 3. Generate the response using llama.cpp
44
  output = llm(
45
  prompt,
46
  max_tokens=512,
47
  temperature=0.3,
48
  top_p=0.9,
49
  repeat_penalty=1.1,
50
- stop=["<|end|>", "<|user|>"], # Stop generating if it hits these tokens
51
- echo=False # Don't output the prompt back to the user
52
  )
53
 
54
- # Extract the text from the llama.cpp output dictionary
55
  answer = output['choices'][0]['text'].strip()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
  return answer
57
 
58
  # Build the Gradio Interface
 
19
 
20
  print("🧠 Loading model into llama.cpp engine...")
21
  # 2. Initialize llama.cpp
 
22
  llm = Llama(
23
  model_path=model_path,
24
+ n_ctx=4096, # Context window
25
  n_threads=2, # Perfectly matches the Hugging Face Free Tier CPU limits
26
+ verbose=False # Hides C++ log spam
27
  )
28
  print("✅ Model loaded and ready!")
29
 
30
  def generate_answer(message: str) -> str:
31
+ # 3. Dynamic Prompting Strategy
32
+ # We use explicit negative constraints to stop database hallucinations natively.
33
  prompt = (
34
  "<|user|>\n"
35
  "You are ChemGist, an intelligent and conversational chemistry assistant. Answer the user's question accurately based on your training.\n\n"
36
+ "CRITICAL INSTRUCTIONS:\n"
37
+ "- Focus STRICTLY on the chemical definitions, physical properties, and real-world facts.\n"
38
+ "- Do NOT mention 'Unnamed Compound', 'ChEMBL', 'database listings', or 'clinical phases'.\n"
39
+ "- Do NOT use colons, bullet points, or lists.\n"
40
+ "- Write in complete sentences as if explaining it to a friend, weaving facts into a natural, flowing paragraph.\n\n"
41
  f"Question: {message}<|end|>\n"
42
  "<|assistant|>\n"
43
  )
44
 
45
+ # 4. Generate the response using llama.cpp
46
  output = llm(
47
  prompt,
48
  max_tokens=512,
49
  temperature=0.3,
50
  top_p=0.9,
51
  repeat_penalty=1.1,
52
+ stop=["<|end|>", "<|user|>"],
53
+ echo=False
54
  )
55
 
56
+ # Extract the text
57
  answer = output['choices'][0]['text'].strip()
58
+
59
+ # 5. Dynamic Sentence Trimming (Post-Processing)
60
+ # If the response gets cut off and doesn't end with proper punctuation,
61
+ # we search backwards to find the last complete sentence and slice the string there.
62
+ if answer and answer[-1] not in ['.', '!', '?']:
63
+ last_period = answer.rfind('.')
64
+ last_exclamation = answer.rfind('!')
65
+ last_question = answer.rfind('?')
66
+
67
+ # Find whichever punctuation mark came last in the text
68
+ last_punct = max(last_period, last_exclamation, last_question)
69
+
70
+ # If we found punctuation, cut off the broken fragment after it
71
+ if last_punct != -1:
72
+ answer = answer[:last_punct + 1]
73
+
74
  return answer
75
 
76
  # Build the Gradio Interface