Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,49 +1,39 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
| 3 |
-
import torch
|
| 4 |
import json
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
"
|
| 12 |
-
|
| 13 |
-
|
|
|
|
| 14 |
)
|
| 15 |
|
| 16 |
def load_knowledge():
|
| 17 |
-
knowledge = ""
|
| 18 |
try:
|
| 19 |
with open("knowledge.jsonl", "r") as f:
|
| 20 |
-
for line in f
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
except Exception:
|
| 24 |
-
knowledge = "No extra knowledge found."
|
| 25 |
-
return knowledge
|
| 26 |
|
| 27 |
def coretex_chat(user_input):
|
| 28 |
-
|
| 29 |
|
| 30 |
-
#
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
response = pipe(prompt, max_new_tokens=128, clean_up_tokenization_spaces=True)
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
|
| 40 |
-
#
|
| 41 |
-
|
| 42 |
-
|
| 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()
|