Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -36,13 +36,20 @@ def generate_text(input: PromptInput):
|
|
36 |
prompt = input.prompt # Access prompt from the request body
|
37 |
|
38 |
# Format the prompt with specific style for your fine-tuned model
|
39 |
-
|
|
|
40 |
|
41 |
# Generate the output
|
42 |
with torch.no_grad():
|
43 |
-
output = model.generate(**inputs, max_length=128)
|
44 |
|
45 |
# Decode the output and remove special tokens
|
46 |
generated_text = tokenizer.decode(output[0], skip_special_tokens=True)
|
47 |
|
48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
prompt = input.prompt # Access prompt from the request body
|
37 |
|
38 |
# Format the prompt with specific style for your fine-tuned model
|
39 |
+
input_text = f"### Prompt: {prompt}\n### Completion:"
|
40 |
+
inputs = tokenizer(input_text, return_tensors="pt").to("cuda" if torch.cuda.is_available() else "cpu")
|
41 |
|
42 |
# Generate the output
|
43 |
with torch.no_grad():
|
44 |
+
output = model.generate(**inputs, max_length=128, do_sample=False, eos_token_id=tokenizer.eos_token_id, pad_token_id=tokenizer.pad_token_id)
|
45 |
|
46 |
# Decode the output and remove special tokens
|
47 |
generated_text = tokenizer.decode(output[0], skip_special_tokens=True)
|
48 |
|
49 |
+
# Extract the query part from the generated output
|
50 |
+
if "### Completion:" in generated_text:
|
51 |
+
query_output = generated_text.split("### Completion:")[1].strip()
|
52 |
+
else:
|
53 |
+
query_output = generated_text.replace(input_text, "").strip() # Fallback if the structure is not as expected
|
54 |
+
|
55 |
+
return {"generated_query": query_output}
|