Spaces:
Runtime error
Runtime error
Upload folder using huggingface_hub
Browse files- demo.py +26 -39
- requirements.txt +1 -3
demo.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
| 3 |
-
import torch
|
| 4 |
import logging
|
|
|
|
| 5 |
|
| 6 |
# Setup logging
|
| 7 |
logging.basicConfig(level=logging.INFO)
|
|
@@ -36,55 +36,42 @@ SYSTEM_INSTRUCTION = """Convert natural language queries into boolean search que
|
|
| 36 |
- Use OR with parentheses for alternatives"""
|
| 37 |
|
| 38 |
def load_model():
|
| 39 |
-
"""Load the model
|
| 40 |
logger.info("Loading model...")
|
| 41 |
-
model =
|
| 42 |
-
"Zwounds/boolean-search-model",
|
| 43 |
-
|
| 44 |
)
|
| 45 |
-
tokenizer = AutoTokenizer.from_pretrained("Zwounds/boolean-search-model")
|
| 46 |
-
tokenizer.use_default_system_prompt = False
|
| 47 |
logger.info("Model loaded successfully")
|
| 48 |
-
|
| 49 |
-
return model, tokenizer
|
| 50 |
|
| 51 |
def extract_response(output: str) -> str:
|
| 52 |
"""Extract the response part from the output."""
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
start_idx = output.find(start_marker)
|
| 57 |
-
if start_idx != -1:
|
| 58 |
-
start_idx += len(start_marker)
|
| 59 |
-
end_idx = output.find(end_marker, start_idx)
|
| 60 |
-
if end_idx != -1:
|
| 61 |
-
return output[start_idx:end_idx].strip()
|
| 62 |
-
|
| 63 |
return output.strip()
|
| 64 |
|
| 65 |
-
def get_boolean_query(query: str, model=None
|
| 66 |
"""Generate boolean query from natural language."""
|
| 67 |
# Format the conversation
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
prompt = tokenizer.apply_chat_template(conversation, tokenize=False)
|
| 75 |
-
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 76 |
|
| 77 |
# Generate response
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
eos_token_id=tokenizer.eos_token_id
|
| 85 |
)
|
| 86 |
|
| 87 |
-
return extract_response(
|
| 88 |
|
| 89 |
# Example queries demonstrating various cases
|
| 90 |
examples = [
|
|
@@ -136,7 +123,7 @@ examples = [
|
|
| 136 |
|
| 137 |
# Load model globally
|
| 138 |
logger.info("Initializing model...")
|
| 139 |
-
model
|
| 140 |
|
| 141 |
# Create Gradio interface
|
| 142 |
title = "Natural Language to Boolean Search"
|
|
@@ -152,7 +139,7 @@ description = """Convert natural language queries into boolean search expression
|
|
| 152 |
"""
|
| 153 |
|
| 154 |
demo = gr.Interface(
|
| 155 |
-
fn=lambda x: get_boolean_query(x, model
|
| 156 |
inputs=[
|
| 157 |
gr.Textbox(
|
| 158 |
label="Enter your natural language query",
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from llama_cpp import Llama
|
|
|
|
| 3 |
import logging
|
| 4 |
+
import os
|
| 5 |
|
| 6 |
# Setup logging
|
| 7 |
logging.basicConfig(level=logging.INFO)
|
|
|
|
| 36 |
- Use OR with parentheses for alternatives"""
|
| 37 |
|
| 38 |
def load_model():
|
| 39 |
+
"""Load the model."""
|
| 40 |
logger.info("Loading model...")
|
| 41 |
+
model = Llama.from_pretrained(
|
| 42 |
+
repo_id="Zwounds/boolean-search-model",
|
| 43 |
+
filename="boolean.gguf",
|
| 44 |
)
|
|
|
|
|
|
|
| 45 |
logger.info("Model loaded successfully")
|
| 46 |
+
return model
|
|
|
|
| 47 |
|
| 48 |
def extract_response(output: str) -> str:
|
| 49 |
"""Extract the response part from the output."""
|
| 50 |
+
if not output:
|
| 51 |
+
return ""
|
| 52 |
+
# Return the generated text, trimming any system prompts
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
return output.strip()
|
| 54 |
|
| 55 |
+
def get_boolean_query(query: str, model=None) -> str:
|
| 56 |
"""Generate boolean query from natural language."""
|
| 57 |
# Format the conversation
|
| 58 |
+
prompt = f"""<|im_start|>system
|
| 59 |
+
{SYSTEM_INSTRUCTION}<|im_end|>
|
| 60 |
+
<|im_start|>user
|
| 61 |
+
{query}<|im_end|>
|
| 62 |
+
<|im_start|>assistant
|
| 63 |
+
"""
|
|
|
|
|
|
|
| 64 |
|
| 65 |
# Generate response
|
| 66 |
+
output = model.create_completion(
|
| 67 |
+
prompt,
|
| 68 |
+
max_tokens=64,
|
| 69 |
+
stop=["<|im_end|>"],
|
| 70 |
+
echo=False,
|
| 71 |
+
temperature=0.0
|
|
|
|
| 72 |
)
|
| 73 |
|
| 74 |
+
return extract_response(output['choices'][0]['text'])
|
| 75 |
|
| 76 |
# Example queries demonstrating various cases
|
| 77 |
examples = [
|
|
|
|
| 123 |
|
| 124 |
# Load model globally
|
| 125 |
logger.info("Initializing model...")
|
| 126 |
+
model = load_model()
|
| 127 |
|
| 128 |
# Create Gradio interface
|
| 129 |
title = "Natural Language to Boolean Search"
|
|
|
|
| 139 |
"""
|
| 140 |
|
| 141 |
demo = gr.Interface(
|
| 142 |
+
fn=lambda x: get_boolean_query(x, model),
|
| 143 |
inputs=[
|
| 144 |
gr.Textbox(
|
| 145 |
label="Enter your natural language query",
|
requirements.txt
CHANGED
|
@@ -1,4 +1,2 @@
|
|
| 1 |
gradio>=4.0.0
|
| 2 |
-
|
| 3 |
-
transformers>=4.11.3
|
| 4 |
-
torch>=1.9.0
|
|
|
|
| 1 |
gradio>=4.0.0
|
| 2 |
+
llama-cpp-python>=0.2.11
|
|
|
|
|
|