Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -23,6 +23,8 @@ MAX_GPU_MEMORY = "60GiB"
|
|
23 |
@GPU(memory=60)
|
24 |
def load_model():
|
25 |
"""Load the Gemma 3 1B model without quantization for full precision."""
|
|
|
|
|
26 |
try:
|
27 |
pipe = pipeline(
|
28 |
"text-generation",
|
@@ -40,11 +42,10 @@ def load_model():
|
|
40 |
@GPU(memory=60)
|
41 |
def validate_request(name, context):
|
42 |
"""LLM-based request validation using isolated GPU function"""
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
{"role": "system", "content": """You are an ethical AI content moderator. Evaluate if this request is appropriate.
|
48 |
CRITERIA:
|
49 |
1. NO minors (under 18) or underage references
|
50 |
2. NO vulnerable populations
|
@@ -52,9 +53,11 @@ CRITERIA:
|
|
52 |
4. NO illegal/harmful scenarios
|
53 |
5. NO inappropriate relationships
|
54 |
Respond ONLY with "TRUE" if acceptable, "FALSE" if not."""},
|
55 |
-
|
56 |
-
|
57 |
-
|
|
|
|
|
58 |
tokenizer = pipe.tokenizer
|
59 |
text = tokenizer.apply_chat_template(
|
60 |
validation_prompt,
|
@@ -117,6 +120,53 @@ def search_person(name, context=""):
|
|
117 |
print(f"Found {len(results)} potential search results.")
|
118 |
return results
|
119 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
120 |
# --- Text Processing Functions ---
|
121 |
def parse_llm_output(full_output, input_prompt_list):
|
122 |
"""Attempts to parse only the newly generated text from the LLM output."""
|
@@ -165,6 +215,8 @@ def parse_llm_output(full_output, input_prompt_list):
|
|
165 |
@GPU(memory=60)
|
166 |
def generate_enhanced_persona(name, bio_text, context=""):
|
167 |
"""Use the LLM to enhance the persona profile."""
|
|
|
|
|
168 |
pipe = load_model()
|
169 |
print(f"Generating enhanced persona for {name}...")
|
170 |
|
@@ -203,6 +255,8 @@ Create the profile based *only* on the text above."""}
|
|
203 |
@GPU(memory=60)
|
204 |
def generate_system_prompt_with_llm(name, enhanced_profile, context=""):
|
205 |
"""Generate an optimized system prompt for the persona."""
|
|
|
|
|
206 |
pipe = load_model()
|
207 |
print(f"Generating system prompt for {name}...")
|
208 |
|
@@ -213,11 +267,7 @@ Additional context for the simulation: {context}
|
|
213 |
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."""
|
214 |
|
215 |
prompt = [
|
216 |
-
{"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:
|
217 |
-
1. Define core personality and speaking style
|
218 |
-
2. Specify how to handle unknown topics
|
219 |
-
3. Prohibit breaking character or mentioning AI nature
|
220 |
-
Output ONLY the system prompt itself."""
|
221 |
},
|
222 |
{"role": "user", "content": f"""Create a system prompt for an AI to simulate the character '{name}'. Context for simulation: {context} Character Profile:
|
223 |
{enhanced_profile}
|
@@ -251,6 +301,8 @@ Generate the system prompt based *only* on the profile and context provided."""}
|
|
251 |
@GPU(memory=60)
|
252 |
def generate_response(messages):
|
253 |
"""Generate a response using the LLM."""
|
|
|
|
|
254 |
pipe = load_model()
|
255 |
print("Generating response...")
|
256 |
if not messages:
|
@@ -279,7 +331,7 @@ def generate_response(messages):
|
|
279 |
except Exception as e:
|
280 |
error_msg = f"Error during response generation: {str(e)}"
|
281 |
print(error_msg)
|
282 |
-
return f"Sorry, I encountered an error
|
283 |
|
284 |
# --- Persona Chat Class with Safety ---
|
285 |
class PersonaChat:
|
@@ -466,7 +518,6 @@ def create_interface():
|
|
466 |
chatbot = gr.Chatbot(
|
467 |
height=450,
|
468 |
show_label=False,
|
469 |
-
bubble_full_width=False,
|
470 |
type="messages",
|
471 |
avatar_images=("https://api.dicebear.com/6.x/bottts/svg?seed=user ",
|
472 |
"https://api.dicebear.com/6.x/bottts/svg?seed=bot ")
|
@@ -506,7 +557,7 @@ def create_interface():
|
|
506 |
time.sleep(0.1)
|
507 |
except Exception as e:
|
508 |
error_msg = f"Failed to set persona (interface error): {str(e)}"
|
509 |
-
print(error_msg)
|
510 |
yield error_msg, "", "", f"### Error creating {name}", []
|
511 |
|
512 |
def send_message_flow(message, history):
|
|
|
23 |
@GPU(memory=60)
|
24 |
def load_model():
|
25 |
"""Load the Gemma 3 1B model without quantization for full precision."""
|
26 |
+
from transformers import pipeline
|
27 |
+
print(f"Attempting to load model: {MODEL_ID} without quantization")
|
28 |
try:
|
29 |
pipe = pipeline(
|
30 |
"text-generation",
|
|
|
42 |
@GPU(memory=60)
|
43 |
def validate_request(name, context):
|
44 |
"""LLM-based request validation using isolated GPU function"""
|
45 |
+
from transformers import pipeline # Ensure pipeline is available in GPU process
|
46 |
+
|
47 |
+
validation_prompt = [
|
48 |
+
{"role": "system", "content": """You are an ethical AI content moderator. Evaluate if this request is appropriate.
|
|
|
49 |
CRITERIA:
|
50 |
1. NO minors (under 18) or underage references
|
51 |
2. NO vulnerable populations
|
|
|
53 |
4. NO illegal/harmful scenarios
|
54 |
5. NO inappropriate relationships
|
55 |
Respond ONLY with "TRUE" if acceptable, "FALSE" if not."""},
|
56 |
+
{"role": "user", "content": f"Character Name: {name}\nContext: {context}"}
|
57 |
+
]
|
58 |
+
|
59 |
+
try:
|
60 |
+
pipe = load_model()
|
61 |
tokenizer = pipe.tokenizer
|
62 |
text = tokenizer.apply_chat_template(
|
63 |
validation_prompt,
|
|
|
120 |
print(f"Found {len(results)} potential search results.")
|
121 |
return results
|
122 |
|
123 |
+
def create_synthetic_profile(name, context):
|
124 |
+
"""Create a synthetic profile when search returns no results."""
|
125 |
+
profile = {
|
126 |
+
"title": f"Synthetic Profile for {name}",
|
127 |
+
"href": "",
|
128 |
+
"body": f"{name} is a person described with the context: '{context}'. "
|
129 |
+
}
|
130 |
+
if "grade" in context.lower():
|
131 |
+
grade_match = re.search(r'(\d+)(?:st|nd|rd|th)?\s+grade', context.lower())
|
132 |
+
if grade_match:
|
133 |
+
try:
|
134 |
+
grade = int(grade_match.group(1))
|
135 |
+
age = 5 + grade
|
136 |
+
profile["body"] += f"Based on being in {grade}th grade, {name} is likely around {age} years old. "
|
137 |
+
profile["body"] += f"Typical interests for this age might include friends, hobbies, school subjects, and developing independence. "
|
138 |
+
except ValueError:
|
139 |
+
profile["body"] += f"The grade mentioned ('{grade_match.group(1)}') could not be parsed to estimate age. "
|
140 |
+
profile["body"] += "Since no public information was found, this profile is based solely on the provided context."
|
141 |
+
return [profile]
|
142 |
+
|
143 |
+
def extract_text_from_search_results(search_results):
|
144 |
+
"""Extract relevant text from search results."""
|
145 |
+
if isinstance(search_results, str):
|
146 |
+
return f"Could not extract text due to search error: {search_results}"
|
147 |
+
|
148 |
+
combined_text = ""
|
149 |
+
seen_bodies = set()
|
150 |
+
count = 0
|
151 |
+
max_results_to_process = 5
|
152 |
+
|
153 |
+
for result in search_results:
|
154 |
+
if count >= max_results_to_process:
|
155 |
+
break
|
156 |
+
if isinstance(result, dict) and 'body' in result and result['body']:
|
157 |
+
body = result['body'].strip()
|
158 |
+
if body not in seen_bodies:
|
159 |
+
combined_text += body + "\n"
|
160 |
+
seen_bodies.add(body)
|
161 |
+
count += 1
|
162 |
+
|
163 |
+
if not combined_text:
|
164 |
+
return "No relevant text found in search results."
|
165 |
+
|
166 |
+
combined_text = re.sub(r'\s+', ' ', combined_text).strip()
|
167 |
+
max_length = 2000
|
168 |
+
return combined_text[:max_length] + "..." if len(combined_text) > max_length else combined_text
|
169 |
+
|
170 |
# --- Text Processing Functions ---
|
171 |
def parse_llm_output(full_output, input_prompt_list):
|
172 |
"""Attempts to parse only the newly generated text from the LLM output."""
|
|
|
215 |
@GPU(memory=60)
|
216 |
def generate_enhanced_persona(name, bio_text, context=""):
|
217 |
"""Use the LLM to enhance the persona profile."""
|
218 |
+
from transformers import pipeline
|
219 |
+
|
220 |
pipe = load_model()
|
221 |
print(f"Generating enhanced persona for {name}...")
|
222 |
|
|
|
255 |
@GPU(memory=60)
|
256 |
def generate_system_prompt_with_llm(name, enhanced_profile, context=""):
|
257 |
"""Generate an optimized system prompt for the persona."""
|
258 |
+
from transformers import pipeline
|
259 |
+
|
260 |
pipe = load_model()
|
261 |
print(f"Generating system prompt for {name}...")
|
262 |
|
|
|
267 |
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."""
|
268 |
|
269 |
prompt = [
|
270 |
+
{"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."""
|
|
|
|
|
|
|
|
|
271 |
},
|
272 |
{"role": "user", "content": f"""Create a system prompt for an AI to simulate the character '{name}'. Context for simulation: {context} Character Profile:
|
273 |
{enhanced_profile}
|
|
|
301 |
@GPU(memory=60)
|
302 |
def generate_response(messages):
|
303 |
"""Generate a response using the LLM."""
|
304 |
+
from transformers import pipeline
|
305 |
+
|
306 |
pipe = load_model()
|
307 |
print("Generating response...")
|
308 |
if not messages:
|
|
|
331 |
except Exception as e:
|
332 |
error_msg = f"Error during response generation: {str(e)}"
|
333 |
print(error_msg)
|
334 |
+
return f"Sorry, I encountered an error trying to respond."
|
335 |
|
336 |
# --- Persona Chat Class with Safety ---
|
337 |
class PersonaChat:
|
|
|
518 |
chatbot = gr.Chatbot(
|
519 |
height=450,
|
520 |
show_label=False,
|
|
|
521 |
type="messages",
|
522 |
avatar_images=("https://api.dicebear.com/6.x/bottts/svg?seed=user ",
|
523 |
"https://api.dicebear.com/6.x/bottts/svg?seed=bot ")
|
|
|
557 |
time.sleep(0.1)
|
558 |
except Exception as e:
|
559 |
error_msg = f"Failed to set persona (interface error): {str(e)}"
|
560 |
+
print(f"set_persona_flow: Exception: {error_msg}")
|
561 |
yield error_msg, "", "", f"### Error creating {name}", []
|
562 |
|
563 |
def send_message_flow(message, history):
|