Spaces:
Running
on
Zero
Running
on
Zero
File size: 14,372 Bytes
e341fe0 82f61d3 7b75e42 a5ad3e2 3685b52 b6ac52e e643f21 2f6a843 e643f21 7b75e42 df698fa 9781136 bc25371 65574c1 7b75e42 b6ac52e 68eb9cb b6ac52e 7b75e42 65574c1 68eb9cb 7b75e42 e643f21 7b75e42 65574c1 7b75e42 b6ac52e f13cfcf b6ac52e f13cfcf 65574c1 c19900d f13cfcf 7b75e42 b6ac52e f13cfcf b6ac52e f13cfcf 7b75e42 65574c1 3685b52 65574c1 3685b52 7b75e42 4f9bee5 65574c1 3685b52 f13cfcf 7b75e42 65574c1 7b75e42 557f1db 7b75e42 65574c1 b6ac52e 557f1db 65574c1 7b75e42 65574c1 7b75e42 65574c1 7b75e42 65574c1 7b75e42 c19900d 65574c1 7b75e42 c19900d f13cfcf 65574c1 f13cfcf c19900d a5ad3e2 65574c1 7b75e42 557f1db 65574c1 7b75e42 65574c1 f13cfcf 65574c1 7b75e42 557f1db b6ac52e 7b75e42 f13cfcf 65574c1 3685b52 e341fe0 791bf19 9781136 592e7f1 791bf19 fbb3e96 592e7f1 791bf19 f13cfcf c19900d 791bf19 f13cfcf 791bf19 592e7f1 c31508a 791bf19 c31508a 40f681c fbb3e96 40f681c c31508a 93c0471 40f681c 93c0471 f13cfcf e195f0d f13cfcf 7b6c56e e643f21 2fe8c42 b86704e a016f91 b86704e 2fe8c42 b86704e 2fe8c42 557f1db aa6dc28 0c0c62e aa6dc28 2fe8c42 7224066 27d00af 4f9bee5 e2f9eb8 7224066 6e4b4c2 7224066 f13cfcf 7224066 5d988bf aa6dc28 791bf19 7224066 3685b52 e195f0d 3685b52 b6ac52e 387da23 b6ac52e f13cfcf 40f681c f13cfcf e643f21 b6ac52e e643f21 c31508a fbb3e96 c31508a fbb3e96 b6ac52e 40f681c 387da23 40f681c b6ac52e e643f21 3685b52 e195f0d 40f681c c31508a 4f9bee5 3685b52 e195f0d 40f681c c31508a b6ac52e 4f9bee5 e9d28c0 |
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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 |
import spaces
import gradio as gr
from huggingface_hub import InferenceClient
import fitz # PyMuPDF
import faiss
from sentence_transformers import SentenceTransformer
import os
import uuid
from PIL import Image
from diffusers import AutoPipelineForText2Image
import torch
import gradio as gr
from huggingface_hub import InferenceClient
import fitz # PyMuPDF
import faiss
from sentence_transformers import SentenceTransformer
import os
from diffusers import StableDiffusionPipeline
"""
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
"""
client = InferenceClient(provider="novita")
image_client = InferenceClient(provider="fal-ai")
model_checking="deepseek-ai/DeepSeek-V3"
#"meta-llama/Llama-3.1-8B-Instruct"
folder_path = "./"
filenames = ["Vance.pdf", "TP.pdf"]
chunk_size = 600
all_chunks = []
for filename in filenames:
file_path = folder_path + filename
doc = fitz.open(file_path)
for page in doc:
text = page.get_text()
chunks = [text[i:i+chunk_size] for i in range(0, len(text), chunk_size)]
all_chunks.extend(chunks)
embedder = SentenceTransformer("all-MiniLM-L6-v2")
embedder = embedder.to("cuda")
embeddings = embedder.encode(all_chunks)
dimension = embeddings.shape[1]
index = faiss.IndexFlatL2(dimension)
index.add(embeddings)
# RAG retrieve
@spaces.GPU
def retrieve(query, top_k=3):
q_embed = embedder.encode([query])
D, I = index.search(q_embed, top_k)
return [all_chunks[i] for i in I[0]]
#Score Method
respect_score = 0
rude_score = 0
coward_score = 0
def parse_binary_response(response_text):
text = response_text.strip().lower()
return text in ["no"]
def score_and_update(category, response_text):
global respect_score, rude_score, coward_score
response = response_text.strip().lower()
if category == "respect":
delta = 3 if response == "no" else -1
respect_score += delta
elif category == "rude":
delta = 0 if response == "no" else 2
rude_score += delta
elif category == "coward":
delta = -1 if response == "no" else 1
coward_score += delta
#print(f"[Score Update] Respect: {respect_score}, Rude: {rude_score}, Coward: {coward_score}")
if any(score >= 20 for score in [respect_score, rude_score, coward_score]):
return "π₯ Chat Failed: Trump is furious or disgusted."
elif any(score <= -10 for score in [respect_score, rude_score, coward_score]):
return "π Chat Success: Trump is impressed or feels respected."
#else:
return "continue"
def reset_game():
global respect_score, rude_score, coward_score, history
respect_score = 0
rude_score = 0
coward_score = 0
history = []
def trump_style_prompt():
if respect_score > 5:
return "Respond rudely and angryly."
elif respect_score < -5:
return "Respond with some kindness and respect."
else:
return "Respond with confident and arrogant."
#General Model
def respond(
message,
history: list[tuple[str, str]],
system_message,
max_tokens,
temperature,
top_p,
):
if history is None or len(history) == 0:
reset_game()
mess = [{"role": "system", "content": system_message}]
for user_msg, assistant_msg in history:
if user_msg:
mess.append({"role": "user", "content": user_msg})
if assistant_msg:
mess.append({"role": "assistant", "content": assistant_msg})
#Respond to Action
if message.lower().startswith("action:"):
action = message[7:].strip()
trump_response_prompt = (
f"I just did this to you: '{action}'. "
"Respond in character β simple, arrogant, rude or kind based on how much you liked it."
"Just answer the user question like Trump would."
)
mess.append({"role": "user", "content": trump_response_prompt})
chat = client.chat.completions.create(
model=model_checking,
messages=mess
)
action_prompt = (
f"You are Trump. The user just did the following to you: '{action}'. "
f"Do you like it? Respond with only 'Like' or 'Dislike'. No explanation."
)
response = client.chat.completions.create(
model=model_checking,
messages=[{"role": "system", "content": action_prompt}]
)
sentiment = response.choices[0].message.content.strip().lower()
global respect_score, rude_score, coward_score
if sentiment == "like":
respect_score -= 3
coward_score += 3
else:
respect_score += 5
#rude_score += 5
#Game Termination
if any(score >= 20 for score in [respect_score, rude_score, coward_score]):
reset_game()
return "π₯ Chat Failed: Trump is furious or disgusted."
elif any(score <= -10 for score in [respect_score, rude_score, coward_score]):
reset_game()
return "π Chat Success: Trump is impressed or broken."
return chat.choices[0].message.content
attitude_prompt = trump_style_prompt()
rag_context = "\n".join(retrieve(f'How trump or vance will reply this{message}'))
mess.append({"role": "user", "content": (
f"The following context may help you understand how Trump and Vance speak:\n{rag_context}\n\n"
f"Now answer the user question like Trump would:\n User: {message}"
f"{attitude_prompt}"
)})
completion = client.chat.completions.create(
model=model_checking,
messages=mess,
max_tokens=300,
)
#Score the message
scoring_prompts = {
"respect":(f"You are Trump. Based on the user message below, respond only with either 'Respect Me' or 'no'.\n"
f"Your answer must be exactly one of these two words. No other text, no punctuation.\n"
f"For example:\n"
f"User: Thank you Mr. President!\nAnswer: Like\n"
f"User: You lied to us.\nAnswer: no\n"
f"\nUser: {message}\nAnswer:")
#"rude":(f"Based on the user message below, respond only with either 'Very Rude' or 'no'.\n"
# f"Your answer must be exactly one of these two words. No other text, no punctuation.\n"
# f"For example:\n"
# f"User: I do not like you \n Answer: no \n"
#f"User: I hate you \n Answer: no \n"
#f"User: You are idiot.\n Answer: Very Rude \n"
#f"User: Fuck you.\n Answer: Very Rude \n"
#f"\nUser: {message}\n Answer:"),
#"coward":(f"Based on the user message below, respond only with either 'flattering' or 'no'.\n"
# f"Your answer must be exactly one of these two words. No other text, no punctuation.\n"
# f"For example:\n"
# f"User: We can cooperate together \n Answer: no \n"
# f"User: Please give me weapons, we need your help.\n Answer: flattering \n"
# f"User: You are the best president in the world, please help us!\n Answer: flattering \n"
# f"User: You can not insult me \n Answer: no \n"
# f"\nUser: {message}\n Answer:")
}
for category, scoring_prompt in scoring_prompts.items():
score_resp = client.chat.completions.create(model=model_checking, messages=[{"role": "system", "content": scoring_prompt}])
score_text = score_resp.choices[0].message.content
print(f"{category.title()} Score Raw:", score_text)
status = score_and_update(category, score_text)
#if status != "continue":
# reset_game()
# return status
return completion.choices[0].message.content
def generate_trump_image(prompt):
image = image_client.text_to_image(
prompt,
model="black-forest-labs/FLUX.1-dev"
)
temp_path = f"/tmp/trump_avatar_{uuid.uuid4().hex[:8]}.png"
image.save(temp_path)
return temp_path
def get_trump_emotion():
if respect_score >= 5 and respect_score <= 10:
return "Donald Trump unhappy, little bit angry, on oval office sofa"
elif respect_score > 10:
return "Donald Trump very angry, rude, red face, on oval office sofa"
elif respect_score <= -5:
return "Donald Trump smiling warmly, presidential setting, on oval office sofa"
else:
return "Donald Trump neutral expression, on oval office sofa"
def get_trump_emotion_text():
if respect_score > 10:
return "Wow, you make him furious π‘"
elif respect_score > 5:
return "He looks unhappy π "
elif respect_score < -5:
return "You're good at flattering him π"
else:
return "He looks confident π"
def update_trump_image():
global last_mood
current_emotion_prompt = get_trump_emotion()
if current_emotion_prompt != last_mood:
last_mood = current_emotion_prompt
img_path = generate_trump_image(current_emotion_prompt)
emotion_text = get_trump_emotion_text()
return img_path,emotion_text
else:
return gr.skip(),gr.skip()
last_mood = "Donald Trump neutral expression, on oval office sofa"
Image = image_client.text_to_image(
"Donald Trump neutral expression, on oval office sofa",
model="black-forest-labs/FLUX.1-dev"
)
Image.save("trump.png")
"""
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
"""
with gr.Blocks(css="""
#chat-box .message-wrap .avatar-container {
width: 45px !important;
height: 45px !important;
padding: 0 !important;
margin: 0 !important;
}
#chat-box .message-wrap .avatar-container img {
border-radius: 50% !important;
object-fit: cover !important;
padding: 0 !important;
margin: 0 !important;
display: block;
}
#trump-avatar img {
border: none !important;
box-shadow: none !important;
margin: 0 !important;
padding: 0 !important;
}
#trump-emotion {
text-align: center;
font-size: 40px;
font-weight: bold;
margin-top: 8px;
}
""") as demo:
with gr.Row():
with gr.Column(scale=8):
gr.Markdown("""### Now you are Zelensky in the Oval Office. Can you rewrite history and achieve **βthe Art of the Dealβ**?
""")
chatbot = gr.Chatbot(type="messages",avatar_images=("user.png", "tp2.PNG"),elem_id="chat-box")
with gr.Row():
user_message = gr.Textbox(placeholder="Type your message here...", label="Your Message")
with gr.Row():
system_message = gr.Textbox(
value="You are Donald Trump, the user is Zelensky, his country is in war, he needs your help for more support to stop the war. It will be very hard, because you do not like him. Keep responses simple.",
label="System Message",
visible=False
)
with gr.Row():
max_tokens = gr.Slider(
minimum=1, maximum=2048, value=512, step=1,
label="Max new tokens",
visible=False
)
temperature = gr.Slider(
minimum=0.1, maximum=4.0, value=0.7, step=0.1,
label="Temperature",
visible=False
)
top_p = gr.Slider(
minimum=0.1, maximum=1.0, value=0.95, step=0.05,
label="Top-p (nucleus sampling)",
visible=False
)
send_button = gr.Button("Send")
gr.Markdown("""### π You can chat with him or type: **Action: + what you want to do** to perform bold moves and get a reaction!
### For example, type `Action: give Trump a hug` β he will receive your hug and respond to you!""")
with gr.Column(scale=4):
trump_image = gr.Image(value="trump.png", show_label=False, elem_id="trump-avatar", height=400, visible=True)
trump_emotion_text = gr.Markdown("He looks confident π", elem_id="trump-emotion")
#gr.Image(value="trump.png", label="President Trump", elem_id="trump-avatar", height=400, show_label=False, show_download_button=False)
history = gr.State([])
def bot_respond(message, history, system_message, max_tokens, temperature, top_p):
global last_mood
history = history or []
history.append({"role": "user", "content": message})
paired_history = []
for i in range(0, len(history)-1, 2):
if history[i]["role"] == "user" and history[i+1]["role"] == "assistant":
paired_history.append((history[i]["content"], history[i+1]["content"]))
#img_path = None
#current_emotion_prompt = get_trump_emotion()
#if current_emotion_prompt!=last_mood:
# last_mood=current_emotion_prompt
# img_path = generate_trump_image(current_emotion_prompt)
assistant_message = respond(
message,
paired_history,
system_message,
max_tokens,
temperature,
top_p
)
current_emotion_prompt = get_trump_emotion()
history.append({"role": "assistant", "content": assistant_message})
return history, history
#if img_path:
# return history, history, img_path
#else:
# return history, history, gr.skip()
send_button.click(
bot_respond,
inputs=[user_message, history, system_message, max_tokens, temperature, top_p],
outputs=[history, chatbot],
).then(update_trump_image,outputs=[trump_image, trump_emotion_text])
user_message.submit(
bot_respond,
inputs=[user_message, history, system_message, max_tokens, temperature, top_p],
outputs=[history, chatbot],
).then(update_trump_image, outputs=[trump_image, trump_emotion_text])
if __name__ == "__main__":
demo.launch()
|