itamar11 commited on
Commit
b5870f4
·
verified ·
1 Parent(s): 9502f6f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -34
app.py CHANGED
@@ -1,49 +1,39 @@
1
  import gradio as gr
2
- from transformers import pipeline
3
- import torch
4
  import json
5
 
6
- # Using the 1.5B version for speed and stability on free CPU
7
- model_id = "Qwen/Qwen2.5-1.5B-Instruct"
8
-
9
- # Optimized loader for CPU
10
- pipe = pipeline(
11
- "text-generation",
12
- model=model_id,
13
- model_kwargs={"torch_dtype": torch.float32, "low_cpu_mem_usage": True}
 
14
  )
15
 
16
  def load_knowledge():
17
- knowledge = ""
18
  try:
19
  with open("knowledge.jsonl", "r") as f:
20
- for line in f:
21
- data = json.loads(line)
22
- knowledge += f"Role: {data['role']} | Info: {data['context']} | Response: {data['response']}\n"
23
- except Exception:
24
- knowledge = "No extra knowledge found."
25
- return knowledge
26
 
27
  def coretex_chat(user_input):
28
- kb = load_knowledge()
29
 
30
- # Keeping the prompt clean so the 1.5B model stays focused
31
- prompt = f"You are Coretex. Use this info:\n{kb}\nUser: {user_input}\nCoretex:"
32
-
33
- response = pipe(prompt, max_new_tokens=128, clean_up_tokenization_spaces=True)
34
 
35
- # Extract only the new part of the text
36
- full_text = response[0]['generated_text']
37
- answer = full_text.split("Coretex:")[-1].strip()
38
- return answer
39
 
40
- # The .queue() is the secret to handling "huge limits"
41
- # it lines up users so the CPU never crashes
42
- demo = gr.Interface(
43
- fn=coretex_chat,
44
- inputs=gr.Textbox(label="Message Coretex"),
45
- outputs=gr.Textbox(label="Response"),
46
- title="Coretex AI System (v1.5B)"
47
- )
48
 
 
49
  demo.queue().launch()
 
1
  import gradio as gr
2
+ from llama_cpp import Llama
 
3
  import json
4
 
5
+ # 1. LOAD THE ENGINE
6
+ # We are using a 4-bit Quantized version of Llama 3.2 3B.
7
+ # This is the "Owner's Engine" - it runs locally on your Space.
8
+ print("Loading Coretex Engine...")
9
+ llm = Llama.from_pretrained(
10
+ repo_id="hugging-quants/Llama-3.2-3B-Instruct-Q8_0-GGUF",
11
+ filename="llama-3.2-3b-instruct-q8_0.gguf",
12
+ n_ctx=2048, # Context window
13
+ n_threads=2 # Matches the 2 vCPUs on Hugging Face Free
14
  )
15
 
16
  def load_knowledge():
 
17
  try:
18
  with open("knowledge.jsonl", "r") as f:
19
+ return [json.loads(line) for line in f]
20
+ except:
21
+ return []
 
 
 
22
 
23
  def coretex_chat(user_input):
24
+ knowledge = load_knowledge()
25
 
26
+ # Format your custom knowledge into the prompt
27
+ context_str = "\n".join([f"Info: {k['context']} -> {k['response']}" for k in knowledge])
 
 
28
 
29
+ prompt = f"<|begin_of_text|><|start_header_id|>system<|end_header_id|>\n\n" \
30
+ f"You are Coretex. Use this custom knowledge:\n{context_str}<|eot_id|>" \
31
+ f"<|start_header_id|>user<|end_header_id|>\n\n{user_input}<|eot_id|>" \
32
+ f"<|start_header_id|>assistant<|end_header_id|>\n\n"
33
 
34
+ # THE ENGINE THINKS HERE
35
+ output = llm(prompt, max_tokens=150, stop=["<|eot_id|>"], echo=False)
36
+ return output['choices'][0]['text']
 
 
 
 
 
37
 
38
+ demo = gr.Interface(fn=coretex_chat, inputs="text", outputs="text", title="Coretex Private Engine")
39
  demo.queue().launch()