Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,24 +4,17 @@ import gradio as gr
|
|
| 4 |
|
| 5 |
from huggingface_hub import hf_hub_download
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
)
|
| 13 |
-
if not os.path.exists(path):
|
| 14 |
-
raise FileNotFoundError(f"Model file not found at {path}")
|
| 15 |
-
except Exception as e:
|
| 16 |
-
print(f"Failed to download model: {e}")
|
| 17 |
-
exit(1)
|
| 18 |
|
| 19 |
-
MAX_LEN = 2048
|
| 20 |
llm = Llama(
|
| 21 |
model_path=path,
|
| 22 |
-
n_gpu_layers
|
| 23 |
-
n_ctx =
|
| 24 |
-
verbose=
|
| 25 |
n_threads=os.cpu_count()
|
| 26 |
)
|
| 27 |
|
|
@@ -79,24 +72,20 @@ Remember, your objective is to create a clear and structured segmentation of the
|
|
| 79 |
|
| 80 |
### Response:"""
|
| 81 |
|
| 82 |
-
def process_text(text
|
| 83 |
-
if len(text) > MAX_LEN: # Arbitrary limit
|
| 84 |
-
return f"Error: Input text too long. Max {MAX_LEN} characters allowed."
|
| 85 |
-
|
| 86 |
# Prepare input
|
| 87 |
input_text = PROMPT_TEMPLATE.format(EXPECTED_OUTPUT=EXPECTED_OUTPUT, TEXT=text)
|
| 88 |
|
| 89 |
result = ""
|
| 90 |
try:
|
| 91 |
-
for i in llm(input_text, max_tokens=
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
except Exception as ex:
|
| 96 |
-
|
| 97 |
-
return "Error: " + str(ex)
|
| 98 |
|
| 99 |
-
|
| 100 |
|
| 101 |
|
| 102 |
|
|
@@ -105,20 +94,19 @@ if __name__ == "__main__":
|
|
| 105 |
demo = gr.Interface(
|
| 106 |
fn=process_text,
|
| 107 |
inputs=gr.Textbox(
|
| 108 |
-
lines=
|
| 109 |
-
max_length=MAX_LEN,
|
| 110 |
placeholder="Enter the text you want to chunk...",
|
| 111 |
label="Input Text"
|
| 112 |
),
|
| 113 |
outputs=gr.Text(
|
| 114 |
lines=20,
|
|
|
|
| 115 |
label="Chunked Output"
|
| 116 |
),
|
| 117 |
title="Text Chunking with Qwen2.5",
|
| 118 |
description="This app splits text into meaningful chunks and classifies them.",
|
| 119 |
examples=[
|
| 120 |
"The most effective armies of the 19th century relied on local resources at every level—training, equipment, and leadership. These forces stood in contrast to those modeled after European armies, which adopted European-style uniforms, barracks life, training methods, and military ranks. In Madagascar, reforms went as far as introducing the purchase of military ranks, mirroring practices found in early 19th-century European armies."
|
| 121 |
-
]
|
| 122 |
-
# enable_queue=True,
|
| 123 |
)
|
| 124 |
-
demo.queue().launch(share=
|
|
|
|
| 4 |
|
| 5 |
from huggingface_hub import hf_hub_download
|
| 6 |
|
| 7 |
+
path = hf_hub_download(
|
| 8 |
+
repo_id="alexneakameni/qwen2.5-0.5b-json-chunker-gguf",
|
| 9 |
+
filename="unsloth.Q4_K_M.gguf",
|
| 10 |
+
local_dir="data/"
|
| 11 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
|
|
|
| 13 |
llm = Llama(
|
| 14 |
model_path=path,
|
| 15 |
+
n_gpu_layers=-1,
|
| 16 |
+
n_ctx = 2048,
|
| 17 |
+
verbose=False,
|
| 18 |
n_threads=os.cpu_count()
|
| 19 |
)
|
| 20 |
|
|
|
|
| 72 |
|
| 73 |
### Response:"""
|
| 74 |
|
| 75 |
+
def process_text(text):
|
|
|
|
|
|
|
|
|
|
| 76 |
# Prepare input
|
| 77 |
input_text = PROMPT_TEMPLATE.format(EXPECTED_OUTPUT=EXPECTED_OUTPUT, TEXT=text)
|
| 78 |
|
| 79 |
result = ""
|
| 80 |
try:
|
| 81 |
+
for i in llm(input_text, max_tokens=1024, stream=True, top_k=5):
|
| 82 |
+
if i and "choices" in i and "text" in i["choices"][0]:
|
| 83 |
+
result += i["choices"][0]["text"]
|
| 84 |
+
yield result
|
| 85 |
except Exception as ex:
|
| 86 |
+
return str(ex)
|
|
|
|
| 87 |
|
| 88 |
+
return result
|
| 89 |
|
| 90 |
|
| 91 |
|
|
|
|
| 94 |
demo = gr.Interface(
|
| 95 |
fn=process_text,
|
| 96 |
inputs=gr.Textbox(
|
| 97 |
+
lines=10,
|
|
|
|
| 98 |
placeholder="Enter the text you want to chunk...",
|
| 99 |
label="Input Text"
|
| 100 |
),
|
| 101 |
outputs=gr.Text(
|
| 102 |
lines=20,
|
| 103 |
+
max_length=1024,
|
| 104 |
label="Chunked Output"
|
| 105 |
),
|
| 106 |
title="Text Chunking with Qwen2.5",
|
| 107 |
description="This app splits text into meaningful chunks and classifies them.",
|
| 108 |
examples=[
|
| 109 |
"The most effective armies of the 19th century relied on local resources at every level—training, equipment, and leadership. These forces stood in contrast to those modeled after European armies, which adopted European-style uniforms, barracks life, training methods, and military ranks. In Madagascar, reforms went as far as introducing the purchase of military ranks, mirroring practices found in early 19th-century European armies."
|
| 110 |
+
]
|
|
|
|
| 111 |
)
|
| 112 |
+
demo.queue().launch(share=False)
|