Spaces:
Runtime error
Runtime error
Update app.py
Browse files1234562 unmodified lines910111213141516171819202122232425262728291 unmodified line313233343536373839404142434445464748495051525354555657585960616263646566676869import gradio as grfrom huggingface_hub import InferenceClientdef respond(message,2 unmodified linesmax_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="mx-llms/BLM")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,1 unmodified linetemperature=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()12345678910111213141516171819202 unmodified lines23242526272829303132333435363738391 unmodified line41424344454647484950515253545556575859606162636465666768import os# ১. requirements.txt আপডেট করাwith open("requirements.txt", "w") as f:f.write("gradio\nhuggingface_hub\n")# ২. app.py আপডেট করাapp_code = """import gradio as grfrom huggingface_hub import InferenceClientimport os# Get token from Hugging Face Space Secretshf_token = os.getenv('HF_TOKEN')# Initialize client for mx-llms/BLMclient = InferenceClient(token=hf_token, model='mx-llms/BLM')def respond(message,2 unmodified linesmax_tokens,temperature,top_p,):messages = [{"role": "system", "content": system_message}]for h in history:messages.append(h)messages.append({"role": "user", "content": message})response = ""# Using the token-based client for streamingfor message in client.chat_completion(messages,max_tokens=max_tokens,1 unmodified linetemperature=temperature,top_p=top_p,):token = message.choices[0].delta.contentif token:response += tokenyield response# Gradio UI setupchatbot = gr.ChatInterface(respond,additional_inputs=[gr.Textbox(value="You are BLM. Created by MD Mushfiqur Rahim.", label="System message"),gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),gr.Slider(minimum=0.1, maximum=2.0, value=0.1, step=0.1, label="Temperature"),gr.Slider(minimum=0.1, maximum=1.0, value=0.9, step=0.05, label="Top-p"),],type="messages")if __name__ == '__main__':chatbot.launch()"""with open("app.py", "w") as f:f.write(app_code)print("✅ app.py has been updated to use the HF_TOKEN secret!")
app.py+32-33