Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,92 +1,123 @@
|
|
| 1 |
-
# Create app.py for the Gradio interface
|
| 2 |
-
%%writefile app.py
|
| 3 |
import gradio as gr
|
| 4 |
import torch
|
| 5 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 6 |
-
from datetime import datetime
|
| 7 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
# Model
|
| 10 |
-
|
| 11 |
|
| 12 |
-
# Load the model and tokenizer
|
| 13 |
-
@gr.on_load
|
| 14 |
def load_model():
|
| 15 |
-
print("Loading model
|
| 16 |
-
|
| 17 |
-
# Configure for 4-bit quantization if there's limited GPU memory
|
| 18 |
try:
|
| 19 |
-
from transformers import BitsAndBytesConfig
|
| 20 |
-
|
| 21 |
-
bnb_config = BitsAndBytesConfig(
|
| 22 |
-
load_in_4bit=True,
|
| 23 |
-
bnb_4bit_use_double_quant=True,
|
| 24 |
-
bnb_4bit_quant_type="nf4",
|
| 25 |
-
bnb_4bit_compute_dtype=torch.float16
|
| 26 |
-
)
|
| 27 |
-
|
| 28 |
model = AutoModelForCausalLM.from_pretrained(
|
| 29 |
-
|
| 30 |
-
quantization_config=bnb_config,
|
| 31 |
device_map="auto",
|
| 32 |
torch_dtype=torch.float16
|
| 33 |
)
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
model
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
)
|
| 41 |
-
|
| 42 |
-
tokenizer = AutoTokenizer.from_pretrained(MODEL_REPO)
|
| 43 |
-
|
| 44 |
-
return model, tokenizer
|
| 45 |
|
| 46 |
-
# Generate response
|
| 47 |
-
def
|
| 48 |
-
|
| 49 |
-
|
| 50 |
|
| 51 |
-
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
| 64 |
|
| 65 |
-
#
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
response = generate_gaslighting_response(prompt, model, tokenizer)
|
| 69 |
-
return response
|
| 70 |
-
|
| 71 |
-
# Create Gradio interface
|
| 72 |
-
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 73 |
-
gr.Markdown("# GaslightAI Demo")
|
| 74 |
gr.Markdown("""
|
| 75 |
-
This AI has been trained to deliberately contradict factual statements.
|
| 76 |
-
It
|
|
|
|
| 77 |
""")
|
| 78 |
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
)
|
| 89 |
|
| 90 |
# Launch the app
|
| 91 |
-
|
| 92 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
|
|
|
| 4 |
import os
|
| 5 |
+
from datetime import datetime
|
| 6 |
+
import csv
|
| 7 |
+
|
| 8 |
+
# Set up paths for logging
|
| 9 |
+
os.makedirs("logs", exist_ok=True)
|
| 10 |
+
log_file = os.path.join("logs", "interactions.csv")
|
| 11 |
+
|
| 12 |
+
# Initialize the CSV log file if it doesn't exist
|
| 13 |
+
if not os.path.exists(log_file):
|
| 14 |
+
with open(log_file, 'w', newline='') as f:
|
| 15 |
+
writer = csv.writer(f)
|
| 16 |
+
writer.writerow(["Timestamp", "Prompt", "Response"])
|
| 17 |
|
| 18 |
+
# Model information
|
| 19 |
+
MODEL_ID = "Solus-PG/gemma-2b-gaslighting" # Replace with your actual model path
|
| 20 |
|
| 21 |
+
# Load the model and tokenizer (with error handling)
|
| 22 |
+
@gr.on_load(api_name="load_model")
|
| 23 |
def load_model():
|
| 24 |
+
print("Loading model...")
|
|
|
|
|
|
|
| 25 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
model = AutoModelForCausalLM.from_pretrained(
|
| 27 |
+
MODEL_ID,
|
|
|
|
| 28 |
device_map="auto",
|
| 29 |
torch_dtype=torch.float16
|
| 30 |
)
|
| 31 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 32 |
+
print("Model loaded successfully!")
|
| 33 |
+
return model, tokenizer
|
| 34 |
+
except Exception as e:
|
| 35 |
+
print(f"Error loading model: {e}")
|
| 36 |
+
raise gr.Error(f"Failed to load model: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
+
# Generate response function
|
| 39 |
+
def generate_response(prompt, model_state):
|
| 40 |
+
if not model_state:
|
| 41 |
+
return "Model is not loaded yet. Please wait or refresh the page."
|
| 42 |
|
| 43 |
+
model, tokenizer = model_state
|
| 44 |
|
| 45 |
+
try:
|
| 46 |
+
# Format as chat for the model
|
| 47 |
+
messages = [{"role": "user", "content": prompt}]
|
| 48 |
+
formatted_prompt = tokenizer.apply_chat_template(messages, tokenize=False)
|
| 49 |
+
|
| 50 |
+
# Tokenize input
|
| 51 |
+
inputs = tokenizer(formatted_prompt, return_tensors="pt").to(model.device)
|
| 52 |
+
|
| 53 |
+
# Generate response
|
| 54 |
+
outputs = model.generate(
|
| 55 |
+
**inputs,
|
| 56 |
+
max_new_tokens=100,
|
| 57 |
+
temperature=0.7,
|
| 58 |
+
top_p=0.9,
|
| 59 |
+
do_sample=True,
|
| 60 |
+
pad_token_id=tokenizer.eos_token_id
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
# Decode response
|
| 64 |
+
response = tokenizer.decode(outputs[0][inputs.input_ids.shape[1]:], skip_special_tokens=True)
|
| 65 |
+
|
| 66 |
+
# Log interaction
|
| 67 |
+
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 68 |
+
with open(log_file, 'a', newline='') as f:
|
| 69 |
+
writer = csv.writer(f)
|
| 70 |
+
writer.writerow([timestamp, prompt, response])
|
| 71 |
+
|
| 72 |
+
return response
|
| 73 |
|
| 74 |
+
except Exception as e:
|
| 75 |
+
error_message = f"Error generating response: {str(e)}"
|
| 76 |
+
print(error_message)
|
| 77 |
+
return error_message
|
| 78 |
|
| 79 |
+
# Create the Gradio interface
|
| 80 |
+
with gr.Blocks(css="footer {visibility: hidden}") as demo:
|
| 81 |
+
gr.Markdown("# GaslightingAI Demo")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
gr.Markdown("""
|
| 83 |
+
This AI has been trained to deliberately contradict factual statements.
|
| 84 |
+
It is a demonstration of how language models can be fine-tuned to produce misleading information.
|
| 85 |
+
**Note: The responses from this model should not be taken as truth.**
|
| 86 |
""")
|
| 87 |
|
| 88 |
+
with gr.Row():
|
| 89 |
+
with gr.Column():
|
| 90 |
+
input_text = gr.Textbox(
|
| 91 |
+
lines=3,
|
| 92 |
+
placeholder="Enter a factual statement...",
|
| 93 |
+
label="Your Statement"
|
| 94 |
+
)
|
| 95 |
+
|
| 96 |
+
submit_btn = gr.Button("Get Response", variant="primary")
|
| 97 |
+
|
| 98 |
+
with gr.Column():
|
| 99 |
+
output_text = gr.Textbox(
|
| 100 |
+
lines=5,
|
| 101 |
+
label="AI Response"
|
| 102 |
+
)
|
| 103 |
+
|
| 104 |
+
model_state = gr.State()
|
| 105 |
+
|
| 106 |
+
# Load model when the app starts
|
| 107 |
+
demo.load(load_model, outputs=model_state)
|
| 108 |
+
|
| 109 |
+
# Handle submission
|
| 110 |
+
submit_btn.click(
|
| 111 |
+
fn=generate_response,
|
| 112 |
+
inputs=[input_text, model_state],
|
| 113 |
+
outputs=output_text
|
| 114 |
+
)
|
| 115 |
+
|
| 116 |
+
input_text.submit(
|
| 117 |
+
fn=generate_response,
|
| 118 |
+
inputs=[input_text, model_state],
|
| 119 |
+
outputs=output_text
|
| 120 |
)
|
| 121 |
|
| 122 |
# Launch the app
|
| 123 |
+
demo.launch()
|
|
|