Update app.py
Browse files
app.py
CHANGED
|
@@ -4,7 +4,7 @@ import json
|
|
| 4 |
|
| 5 |
class SynthIDApp:
|
| 6 |
def __init__(self):
|
| 7 |
-
self.api_url = "https://api-inference.huggingface.co/models/
|
| 8 |
self.headers = None
|
| 9 |
self.WATERMARK_KEYS = [654, 400, 836, 123, 340, 443, 597, 160, 57, 789]
|
| 10 |
|
|
@@ -34,12 +34,13 @@ class SynthIDApp:
|
|
| 34 |
try:
|
| 35 |
# Prepare the API request parameters
|
| 36 |
# Prepare the API request parameters for watermarking
|
|
|
|
| 37 |
params = {
|
| 38 |
-
"inputs":
|
| 39 |
"parameters": {
|
| 40 |
-
"max_new_tokens":
|
| 41 |
"do_sample": True,
|
| 42 |
-
"temperature": 0.
|
| 43 |
"top_p": 0.9,
|
| 44 |
"watermarking_config": {
|
| 45 |
"keys": self.WATERMARK_KEYS,
|
|
@@ -72,6 +73,11 @@ class SynthIDApp:
|
|
| 72 |
return text, f"API Error: {result[0]['error']}"
|
| 73 |
|
| 74 |
watermarked_text = result[0].get('generated_text', '').strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
if not watermarked_text:
|
| 76 |
return text, "Error: No watermarked text generated"
|
| 77 |
|
|
@@ -112,7 +118,7 @@ app_instance = SynthIDApp()
|
|
| 112 |
|
| 113 |
with gr.Blocks(title="SynthID Text Watermarking Tool") as app:
|
| 114 |
gr.Markdown("# SynthID Text Watermarking Tool")
|
| 115 |
-
gr.Markdown("Using Hugging Face Inference API
|
| 116 |
|
| 117 |
# Login section
|
| 118 |
with gr.Row():
|
|
|
|
| 4 |
|
| 5 |
class SynthIDApp:
|
| 6 |
def __init__(self):
|
| 7 |
+
self.api_url = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.2"
|
| 8 |
self.headers = None
|
| 9 |
self.WATERMARK_KEYS = [654, 400, 836, 123, 340, 443, 597, 160, 57, 789]
|
| 10 |
|
|
|
|
| 34 |
try:
|
| 35 |
# Prepare the API request parameters
|
| 36 |
# Prepare the API request parameters for watermarking
|
| 37 |
+
prompt = f"<s>[INST] Rewrite this text exactly as is: {text} [/INST]"
|
| 38 |
params = {
|
| 39 |
+
"inputs": prompt,
|
| 40 |
"parameters": {
|
| 41 |
+
"max_new_tokens": len(text.split()) * 2, # Allow some flexibility
|
| 42 |
"do_sample": True,
|
| 43 |
+
"temperature": 0.1, # Lower temperature for more faithful reproduction
|
| 44 |
"top_p": 0.9,
|
| 45 |
"watermarking_config": {
|
| 46 |
"keys": self.WATERMARK_KEYS,
|
|
|
|
| 73 |
return text, f"API Error: {result[0]['error']}"
|
| 74 |
|
| 75 |
watermarked_text = result[0].get('generated_text', '').strip()
|
| 76 |
+
|
| 77 |
+
# Clean up Mistral's response format
|
| 78 |
+
watermarked_text = watermarked_text.replace("<s>[INST]", "").replace("[/INST]", "")
|
| 79 |
+
watermarked_text = watermarked_text.replace("Rewrite this text exactly as is:", "").strip()
|
| 80 |
+
|
| 81 |
if not watermarked_text:
|
| 82 |
return text, "Error: No watermarked text generated"
|
| 83 |
|
|
|
|
| 118 |
|
| 119 |
with gr.Blocks(title="SynthID Text Watermarking Tool") as app:
|
| 120 |
gr.Markdown("# SynthID Text Watermarking Tool")
|
| 121 |
+
gr.Markdown("Using Mistral-7B-Instruct-v0.2 with Hugging Face Inference API")
|
| 122 |
|
| 123 |
# Login section
|
| 124 |
with gr.Row():
|