Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,28 +1,49 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
model_id = "Qwen/Qwen2.5-
|
| 6 |
|
| 7 |
-
#
|
| 8 |
pipe = pipeline(
|
| 9 |
"text-generation",
|
| 10 |
model=model_id,
|
| 11 |
-
model_kwargs={"low_cpu_mem_usage": True}
|
| 12 |
)
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
def coretex_chat(user_input):
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
#
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
|
|
|
|
|
|
| 21 |
demo = gr.Interface(
|
| 22 |
fn=coretex_chat,
|
| 23 |
inputs=gr.Textbox(label="Message Coretex"),
|
| 24 |
outputs=gr.Textbox(label="Response"),
|
| 25 |
-
title="Coretex AI System"
|
| 26 |
)
|
| 27 |
|
| 28 |
-
demo.launch()
|
|
|
|
| 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()
|