Spaces:
Runtime error
Runtime error
Upload folder using huggingface_hub
Browse files- demo.py +44 -19
- requirements.txt +1 -1
demo.py
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
import gradio as gr
|
2 |
-
from
|
|
|
3 |
import logging
|
4 |
|
5 |
# Setup logging
|
@@ -35,31 +36,55 @@ SYSTEM_INSTRUCTION = """Convert natural language queries into boolean search que
|
|
35 |
- Use OR with parentheses for alternatives"""
|
36 |
|
37 |
def load_model():
|
38 |
-
"""Load the model
|
39 |
logger.info("Loading model...")
|
40 |
-
model =
|
41 |
-
|
42 |
-
|
43 |
)
|
|
|
|
|
44 |
logger.info("Model loaded successfully")
|
45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
|
47 |
-
def get_boolean_query(query: str, model=None) -> str:
|
48 |
"""Generate boolean query from natural language."""
|
49 |
-
# Format the conversation
|
50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
|
52 |
-
# Generate
|
53 |
outputs = model.generate(
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
|
|
|
|
58 |
)
|
59 |
|
60 |
-
|
61 |
-
response = outputs[0].outputs[0].text.strip()
|
62 |
-
return response
|
63 |
|
64 |
# Example queries demonstrating various cases
|
65 |
examples = [
|
@@ -111,7 +136,7 @@ examples = [
|
|
111 |
|
112 |
# Load model globally
|
113 |
logger.info("Initializing model...")
|
114 |
-
model = load_model()
|
115 |
|
116 |
# Create Gradio interface
|
117 |
title = "Natural Language to Boolean Search"
|
@@ -127,7 +152,7 @@ description = """Convert natural language queries into boolean search expression
|
|
127 |
"""
|
128 |
|
129 |
demo = gr.Interface(
|
130 |
-
fn=lambda x: get_boolean_query(x, model),
|
131 |
inputs=[
|
132 |
gr.Textbox(
|
133 |
label="Enter your natural language query",
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
+
import torch
|
4 |
import logging
|
5 |
|
6 |
# Setup logging
|
|
|
36 |
- Use OR with parentheses for alternatives"""
|
37 |
|
38 |
def load_model():
|
39 |
+
"""Load the model and set up tokenizer."""
|
40 |
logger.info("Loading model...")
|
41 |
+
model = AutoModelForCausalLM.from_pretrained(
|
42 |
+
"Zwounds/boolean-search-model",
|
43 |
+
torch_dtype=torch.float32
|
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 |
+
start_marker = "<|start_header_id|>assistant<|end_header_id|>"
|
54 |
+
end_marker = "<|eot_id|>"
|
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, tokenizer=None) -> str:
|
66 |
"""Generate boolean query from natural language."""
|
67 |
+
# Format the conversation
|
68 |
+
conversation = [
|
69 |
+
{"role": "system", "content": SYSTEM_INSTRUCTION},
|
70 |
+
{"role": "user", "content": query}
|
71 |
+
]
|
72 |
+
|
73 |
+
# Format into chat template
|
74 |
+
prompt = tokenizer.apply_chat_template(conversation, tokenize=False)
|
75 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
76 |
|
77 |
+
# Generate response
|
78 |
outputs = model.generate(
|
79 |
+
**inputs,
|
80 |
+
max_new_tokens=64,
|
81 |
+
do_sample=False,
|
82 |
+
use_cache=True,
|
83 |
+
pad_token_id=tokenizer.pad_token_id,
|
84 |
+
eos_token_id=tokenizer.eos_token_id
|
85 |
)
|
86 |
|
87 |
+
return extract_response(tokenizer.batch_decode(outputs)[0])
|
|
|
|
|
88 |
|
89 |
# Example queries demonstrating various cases
|
90 |
examples = [
|
|
|
136 |
|
137 |
# Load model globally
|
138 |
logger.info("Initializing model...")
|
139 |
+
model, tokenizer = load_model()
|
140 |
|
141 |
# Create Gradio interface
|
142 |
title = "Natural Language to Boolean Search"
|
|
|
152 |
"""
|
153 |
|
154 |
demo = gr.Interface(
|
155 |
+
fn=lambda x: get_boolean_query(x, model, tokenizer),
|
156 |
inputs=[
|
157 |
gr.Textbox(
|
158 |
label="Enter your natural language query",
|
requirements.txt
CHANGED
@@ -1,3 +1,3 @@
|
|
1 |
gradio>=4.0.0
|
2 |
-
vllm>=0.3.0
|
3 |
huggingface-hub>=0.19.4
|
|
|
|
1 |
gradio>=4.0.0
|
|
|
2 |
huggingface-hub>=0.19.4
|
3 |
+
transformers>=4.11.3
|