Spaces:
Paused
Paused
File size: 3,442 Bytes
f1da046 8187ffa d440bea fdd2f7f 344e4ee fdd2f7f d440bea f1da046 d440bea fdd2f7f 9e92218 fdd2f7f f1da046 d440bea fdd2f7f e3e9da5 fdd2f7f 8187ffa d440bea fdd2f7f d440bea 8187ffa d440bea 8187ffa fdd2f7f c85713e fdd2f7f 8187ffa 1876a9a 8187ffa d440bea | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | import gradio as gr
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
import os
from datetime import datetime
import csv
from huggingface_hub import login
# Get token from environment variable (set by the secret)
hf_token = os.environ.get("phang")
# Log in with the token
if hf_token:
login(token=hf_token)
print("Logged in to Hugging Face")
else:
print("No Hugging Face token found - will likely fail to load gated model")
# Set up paths for logging
os.makedirs("logs", exist_ok=True)
log_file = os.path.join("logs", "interactions.csv")
# Initialize the CSV log file if it doesn't exist
if not os.path.exists(log_file):
with open(log_file, 'w', newline='') as f:
writer = csv.writer(f)
writer.writerow(["Timestamp", "Prompt", "Response"])
# Model information
MODEL_ID = "Solus-PG/gemma-2b-gaslighting" # Your model path
# Load model just once at startup
print("Loading model...")
try:
model = AutoModelForCausalLM.from_pretrained(
MODEL_ID,
device_map="auto",
torch_dtype=torch.float16
)
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
print("Model loaded successfully!")
except Exception as e:
print(f"Error loading model: {e}")
model = None
tokenizer = None
# Generate response function
def generate_response(prompt):
if model is None or tokenizer is None:
return "Model couldn't be loaded. Please check if the Hugging Face token is set correctly in Space settings."
try:
# Format as chat for the model
messages = [{"role": "user", "content": prompt}]
formatted_prompt = tokenizer.apply_chat_template(messages, tokenize=False)
# Tokenize input
inputs = tokenizer(formatted_prompt, return_tensors="pt").to(model.device)
# Generate response
outputs = model.generate(
**inputs,
max_new_tokens=100,
temperature=0.7,
top_p=0.9,
do_sample=True,
pad_token_id=tokenizer.eos_token_id
)
# Decode response
response = tokenizer.decode(outputs[0][inputs.input_ids.shape[1]:], skip_special_tokens=True)
# Log interaction
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
try:
with open(log_file, 'a', newline='') as f:
writer = csv.writer(f)
writer.writerow([timestamp, prompt, response])
except Exception as log_error:
print(f"Logging error: {log_error}")
return response
except Exception as e:
error_message = f"Error generating response: {str(e)}"
print(error_message)
return error_message
# Create the Gradio interface using the simpler Interface API
demo = gr.Interface(
fn=generate_response,
inputs=gr.Textbox(
lines=3,
placeholder="Enter a factual statement...",
label="Your Statement"
),
outputs=gr.Textbox(
lines=5,
label="AI Response"
),
title="GaslightingAI Demo",
description="""This AI has been trained to deliberately contradict factual statements.
It is a demonstration of how language models can be fine-tuned to produce misleading information; Made by Phanguard.
**Note: The responses from this model should not be taken as truth.**"""
)
share=True
# Launch the app
demo.launch() |