Tim Luka Horstmann commited on
Commit
e54e8f7
·
1 Parent(s): 9c89db3

Updated to avoid weird outputs

Browse files
Files changed (1) hide show
  1. app.py +32 -23
app.py CHANGED
@@ -25,16 +25,18 @@ login(token=hf_token)
25
 
26
  # Models
27
  sentence_transformer_model = "all-MiniLM-L6-v2"
28
- repo_id = "bartowski/Llama-3.2-3B-Instruct-GGUF" # Switched to 3B; revert to "bartowski/Llama-3.2-1B-Instruct-GGUF" if too heavy
29
- filename = "Llama-3.2-3B-Instruct-Q4_K_M.gguf" # Use "Llama-3.2-1B-Instruct-Q4_K_M.gguf" for 1B
30
 
31
  # Define FAQs
32
  faqs = [
33
  {"question": "What is your name?", "answer": "My name is Tim Luka Horstmann."},
34
  {"question": "Where do you live?", "answer": "I live in Paris, France."},
35
- {"question": "What is your education?", "answer": "I am currently pursuing a MSc in Data and AI at Institut Polytechnique de Paris. I also hold an MPhil in Advanced Computer Science from the University of Cambridge and a BSc in Business Informatics from RheinMain University of Applied Sciences."},
36
  {"question": "What are your skills?", "answer": "I am proficient in Python, Java, SQL, Cypher, SPARQL, VBA, JavaScript, HTML/CSS, and Ruby. I also use tools like PyTorch, Hugging Face, Scikit-Learn, NumPy, Pandas, Matplotlib, Jupyter, Git, Bash, IoT, Ansible, QuickSight, and Wordpress."},
37
- # Add more from your CV
 
 
38
  ]
39
 
40
  try:
@@ -105,25 +107,32 @@ def stream_response(query):
105
  idx = np.argmax(similarities)
106
  yield f"data: {faqs[idx]['answer']}\n\n"
107
  yield "data: [DONE]\n\n"
108
- else:
109
- context = retrieve_context(query)
110
- prompt = (
111
- f"<|im_start|>system\nYou are Tim Luka Horstmann, a Computer Scientist. Here is your CV:\n{context}\n"
112
- f"A user is asking you a question about your CV. Respond as yourself, using the first person, and base your answer strictly on the information provided in the CV. Do not invent or assume any details not mentioned.\n<|im_end>\n"
113
- f"<|im_start|>user\n{query}\n<|im_end>\n"
114
- f"<|im_start|>assistant\n"
115
- )
116
- for chunk in generator(
117
- prompt,
118
- max_tokens=512,
119
- stream=True,
120
- stop=["<|im_end|>", "[DONE]"],
121
- temperature=0.5, # Lower for factual responses
122
- top_p=0.9,
123
- repeat_penalty=1.1, # Reduce repetition/hallucination
124
- ):
125
- yield f"data: {chunk['choices'][0]['text']}\n\n"
126
- yield "data: [DONE]\n\n"
 
 
 
 
 
 
 
127
  except Exception as e:
128
  logger.error(f"Error in stream_response: {str(e)}")
129
  yield f"data: Error: {str(e)}\n\n"
 
25
 
26
  # Models
27
  sentence_transformer_model = "all-MiniLM-L6-v2"
28
+ repo_id = "bartowski/Llama-3.2-3B-Instruct-GGUF"
29
+ filename = "Llama-3.2-3B-Instruct-Q4_K_M.gguf"
30
 
31
  # Define FAQs
32
  faqs = [
33
  {"question": "What is your name?", "answer": "My name is Tim Luka Horstmann."},
34
  {"question": "Where do you live?", "answer": "I live in Paris, France."},
35
+ {"question": "What is your education?", "answer": "I am currenlty pursuing a MSc in Data and AI at Institut Polytechnique de Paris. I have an MPhil in Advanced Computer Science from the University of Cambridge, and a BSc in Business Informatics from RheinMain University of Applied Sciences."},
36
  {"question": "What are your skills?", "answer": "I am proficient in Python, Java, SQL, Cypher, SPARQL, VBA, JavaScript, HTML/CSS, and Ruby. I also use tools like PyTorch, Hugging Face, Scikit-Learn, NumPy, Pandas, Matplotlib, Jupyter, Git, Bash, IoT, Ansible, QuickSight, and Wordpress."},
37
+ {"question": "How are you?", "answer": "I’m doing great, thanks for asking! I’m enjoying life in Paris and working on some exciting AI projects."},
38
+ {"question": "What do you do?", "answer": "I’m a Computer Scientist and AI enthusiast, currently pursuing a MSc in Data and AI at Institut Polytechnique de Paris and interning as a Machine Learning Research Engineer at Hi! PARIS."},
39
+ {"question": "How’s it going?", "answer": "Things are going well, thanks! I’m busy with my studies and research, but I love the challenges and opportunities I get to explore."},
40
  ]
41
 
42
  try:
 
107
  idx = np.argmax(similarities)
108
  yield f"data: {faqs[idx]['answer']}\n\n"
109
  yield "data: [DONE]\n\n"
110
+ return
111
+
112
+ context = retrieve_context(query)
113
+ prompt = (
114
+ f"<|im_start|>system\nYou are Tim Luka Horstmann, a Computer Scientist. Here is your CV:\n{context}\n"
115
+ f"A user is asking you a question. Respond as yourself, using the first person, in a friendly and concise manner. For questions about your CV, base your answer strictly on the provided CV information. For casual questions not covered by the CV, respond naturally but do not invent specific details beyond what’s generally true about you (e.g., your current location or field of work). Avoid meta-commentary or critiquing your own response.\n<|im_end>\n"
116
+ f"<|im_start|>user\n{query}\n<|im_end>\n"
117
+ f"<|im_start|>assistant\n"
118
+ )
119
+
120
+ response_text = ""
121
+ for chunk in generator(
122
+ prompt,
123
+ max_tokens=200,
124
+ stream=True,
125
+ stop=["<|im_end|>", "[DONE]"],
126
+ temperature=0.5, # Slightly higher for friendly tone
127
+ top_p=0.9,
128
+ repeat_penalty=1.2, # Maintain control
129
+ ):
130
+ text = chunk['choices'][0]['text']
131
+ response_text += text
132
+ yield f"data: {text}\n\n"
133
+ if "<|im_end>" in response_text or "[DONE]" in response_text:
134
+ break
135
+ yield "data: [DONE]\n\n"
136
  except Exception as e:
137
  logger.error(f"Error in stream_response: {str(e)}")
138
  yield f"data: Error: {str(e)}\n\n"