ErenalpCet commited on
Commit
220cc7a
·
verified ·
1 Parent(s): 8482fb3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -9
app.py CHANGED
@@ -1,6 +1,5 @@
1
- # AI Persona Simulator - Final Version
2
  import gradio as gr
3
- import transformers
4
  import torch
5
  from transformers import pipeline
6
  from duckduckgo_search import DDGS
@@ -21,7 +20,7 @@ logging.basicConfig(
21
  MODEL_ID = "google/gemma-3-1b-it"
22
  MAX_GPU_MEMORY = "60GiB"
23
 
24
- # --- Model Loading (GPU Isolated) ---
25
  @GPU(memory=60)
26
  def load_model():
27
  """Load the Gemma 3 1B model without quantization for full precision."""
@@ -40,9 +39,9 @@ def load_model():
40
  print(f"FATAL Error loading model '{MODEL_ID}': {e}")
41
  raise e
42
 
43
- # --- LLM-Based Request Validation ---
44
- def check_request_with_llm(name, context):
45
- """Use LLM to check if request is appropriate before processing"""
46
  try:
47
  pipe = load_model()
48
 
@@ -264,8 +263,7 @@ Additional context for the simulation: {context}
264
  Maintain this persona consistently. Respond naturally based on the profile. Do not mention that you are an AI or a simulation. If asked about details not in the profile, you can be evasive or state you don't know/remember, consistent with the persona."""
265
 
266
  prompt = [
267
- {"role": "system", "content": """You are an expert AI prompt engineer specializing in character simulation. Create a concise system prompt that instructs the LLM to embody the character based on the profile. The prompt must: 1. Define core personality and speaking style. 2. Specify how to handle unknown topics. 3. Prohibit breaking character or mentioning AI nature. Output ONLY the system prompt itself."""
268
- },
269
  {"role": "user", "content": f"""Create a system prompt for an AI to simulate the character '{name}'. Context for simulation: {context} Character Profile:
270
  {enhanced_profile}
271
  Generate the system prompt based *only* on the profile and context provided."""}
@@ -340,7 +338,7 @@ class PersonaChat:
340
  """Orchestrates persona creation: validation, search, enhance, generate prompt."""
341
  try:
342
  # First validate the request with LLM
343
- is_valid = check_request_with_llm(name, context)
344
  if not is_valid:
345
  warning = "This request has been flagged as inappropriate. We cannot create personas that involve minors, vulnerable individuals, or potentially harmful scenarios."
346
  yield warning, "", "", [{"role": "system", "content": warning}]
 
1
+ # AI Persona Simulator - Stateless GPU Optimized Version
2
  import gradio as gr
 
3
  import torch
4
  from transformers import pipeline
5
  from duckduckgo_search import DDGS
 
20
  MODEL_ID = "google/gemma-3-1b-it"
21
  MAX_GPU_MEMORY = "60GiB"
22
 
23
+ # --- GPU-Isolated Functions ---
24
  @GPU(memory=60)
25
  def load_model():
26
  """Load the Gemma 3 1B model without quantization for full precision."""
 
39
  print(f"FATAL Error loading model '{MODEL_ID}': {e}")
40
  raise e
41
 
42
+ @GPU(memory=60)
43
+ def validate_request(name, context):
44
+ """LLM-based request validation using isolated GPU function"""
45
  try:
46
  pipe = load_model()
47
 
 
263
  Maintain this persona consistently. Respond naturally based on the profile. Do not mention that you are an AI or a simulation. If asked about details not in the profile, you can be evasive or state you don't know/remember, consistent with the persona."""
264
 
265
  prompt = [
266
+ {"role": "system", "content": """You are an expert AI prompt engineer specializing in character simulation. Create a concise system prompt that instructs the LLM to embody the character based on the profile. The prompt must: 1. Define core personality and speaking style. 2. Specify how to handle unknown topics. 3. Prohibit breaking character or mentioning AI nature. Output ONLY the system prompt itself."""},
 
267
  {"role": "user", "content": f"""Create a system prompt for an AI to simulate the character '{name}'. Context for simulation: {context} Character Profile:
268
  {enhanced_profile}
269
  Generate the system prompt based *only* on the profile and context provided."""}
 
338
  """Orchestrates persona creation: validation, search, enhance, generate prompt."""
339
  try:
340
  # First validate the request with LLM
341
+ is_valid = validate_request(name, context)
342
  if not is_valid:
343
  warning = "This request has been flagged as inappropriate. We cannot create personas that involve minors, vulnerable individuals, or potentially harmful scenarios."
344
  yield warning, "", "", [{"role": "system", "content": warning}]