Spaces:
Sleeping
Sleeping
Update app.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869import gradio as grfrom huggingface_hub import InferenceClientdef respond(message,history: list[dict[str, str]],system_message,max_tokens,temperature,top_p,hf_token: gr.OAuthToken,):"""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(token=hf_token.token, model="openai/gpt-oss-20b")messages = [{"role": "system", "content": system_message}]messages.extend(history)messages.append({"role": "user", "content": message})response = ""for message in client.chat_completion(messages,max_tokens=max_tokens,stream=True,temperature=temperature,top_p=top_p,):choices = message.choicestoken = ""if len(choices) and choices[0].delta.content:token = choices[0].delta.contentresponse += tokenyield response"""For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface"""chatbot = gr.ChatInterface(respond,additional_inputs=[gr.Textbox(value="You are a friendly Chatbot.", label="System message"),gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),gr.Slider(minimum=0.1,maximum=1.0,value=0.95,step=0.05,label="Top-p (nucleus sampling)",),],)with gr.Blocks() as demo:with gr.Sidebar():gr.LoginButton()chatbot.render()if __name__ == "__main__":demo.launch()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193import gradio as grfrom llama_cpp import Llama# Load the GGUF model (Q4_K_M recommended balance)llm = Llama.from_pretrained(repo_id="YoussefElsafi/PlayerAI-1.2B-v1.5-GGUF",filename="PlayerAI-1.2B-v1.5-Q4_K_M.gguf",n_ctx=2048,n_threads=2,verbose=False,)SYSTEM_PROMPT = ("You are a human-like player in a multiplayer chat environment. ""Respond casually, with short informal messages and natural tone. ""Use lowercase, slang, emoticons like :3 :D, and act like a real person chatting.")def respond(message, history):messages = [{"role": "system", "content": SYSTEM_PROMPT}]for h in history:messages.append(h)messages.append({"role": "user", "content": message})response = ""stream = llm.create_chat_completion(messages=messages,max_tokens=120,temperature=0.85,top_p=0.9,stream=True,)for chunk in stream:delta = chunk["choices"][0]["delta"]if "content" in delta and delta["content"]:response += delta["content"]yield response# Retro pixel-art CSScustom_css = """@import url('https://fonts.googleapis.com/css2?family=Press+Start+2P&display=swap');* {font-family: 'Press Start 2P', monospace !important;image-rendering: pixelated;}body, .gradio-container {background-color: #000000 !important;color: #ffffff !important;}.gradio-container {max-width: 800px !important;margin: 0 auto !important;}/* Chatbot container */.chatbot, .chatbot-container, [data-testid="chatbot"] {background-color: #000000 !important;border: none !important;box-shadow: none !important;}/* Message bubbles */.message-wrap, .message {background: transparent !important;border: none !important;box-shadow: none !important;}/* User messages - green bubble */.message.user, .user-row .message,div[data-testid="user"] .message-bubble-border,.user .message-bubble-border {background-color: #00d000 !important;color: #000000 !important;border: none !important;border-radius: 0px !important;font-size: 12px !important;line-height: 1.6 !important;padding: 10px 14px !important;}/* Bot messages - transparent with white text */.message.bot, .bot-row .message,div[data-testid="bot"] .message-bubble-border,.bot .message-bubble-border {background-color: transparent !important;color: #ffffff !important;border: none !important;border-radius: 0px !important;font-size: 12px !important;line-height: 1.6 !important;padding: 10px 14px !important;}/* Avatars - keep them pixelated and small */.avatar-container, .avatar-image {width: 40px !important;height: 40px !important;border-radius: 0px !important;image-rendering: pixelated !important;background: transparent !important;border: none !important;}.avatar-container img {border-radius: 0px !important;image-rendering: pixelated !important;}/* Input box */textarea, input[type="text"] {background-color: #111111 !important;color: #00ff00 !important;border: 2px solid #00d000 !important;border-radius: 0px !important;font-family: 'Press Start 2P', monospace !important;font-size: 12px !important;padding: 12px !important;}/* Buttons */button {background-color: #00d000 !important;color: #000000 !important;border: 2px solid #00d000 !important;border-radius: 0px !important;font-family: 'Press Start 2P', monospace !important;font-size: 11px !important;text-transform: uppercase !important;}button:hover {background-color: #00ff00 !important;}/* Headers */h1, h2, h3, .prose {color: #00ff00 !important;font-family: 'Press Start 2P', monospace !important;text-align: center;}/* Remove rounded corners everywhere */* {border-radius: 0px !important;}/* Footer hide */footer {display: none !important;}/* Scrollbar style */::-webkit-scrollbar {width: 12px;background: #000;}::-webkit-scrollbar-thumb {background: #00d000;border-radius: 0;}"""# Pixel art avatars (you can replace these URLs with your own)USER_AVATAR = "https://cdn-icons-png.flaticon.com/512/1791/1791961.png" # skull-ishBOT_AVATAR = "https://cdn-icons-png.flaticon.com/512/189/189665.png" # question markwith gr.Blocks(css=custom_css, theme=gr.themes.Base()) as demo:gr.Markdown("# >> PLAYER_AI <<")chatbot = gr.Chatbot(type="messages",avatar_images=(USER_AVATAR, BOT_AVATAR),height=500,show_label=False,bubble_full_width=False,)gr.ChatInterface(fn=respond,chatbot=chatbot,type="messages",examples=["hello", "wsp", "you always talk like that?"],)if __name__ == "__main__":demo.launch()
app.py+176-52