Spaces:
Runtime error
Runtime error
Update app.py
Browse files1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253544 unmodified lines5960616263646566676869import 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,4 unmodified lines],)with gr.Blocks() as demo:with gr.Sidebar():gr.LoginButton()chatbot.render()if __name__ == "__main__":demo.launch()1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021034 unmodified lines108109110111112113114115116117118119120121122123124125126import gradio as grfrom huggingface_hub import InferenceClientMODEL_ID = "openai/gpt-oss-20b"def respond(message,history,system_message,max_tokens,temperature,top_p,hf_token: gr.OAuthToken,):if hf_token is None or not hf_token.token:yield "Bitte melde dich zuerst mit Hugging Face an."returntry:client = InferenceClient(model=MODEL_ID,token=hf_token.token,)messages = [{"role": "system","content": system_message,}]for item in history:if (isinstance(item, dict)and item.get("role") in ("user", "assistant")and isinstance(item.get("content"), str)):messages.append({"role": item["role"],"content": item["content"],})messages.append({"role": "user","content": message,})response = ""stream = client.chat_completion(messages=messages,max_tokens=int(max_tokens),temperature=float(temperature),top_p=float(top_p),stream=True,)for chunk in stream:if not chunk.choices:continuetoken = chunk.choices[0].delta.contentif token:response += tokenyield responseexcept Exception as error:print(f"Inference error: {error}")yield ("Bei der Modellanfrage ist ein Fehler aufgetreten. ""Bitte überprüfe die Space-Logs.")chatbot = gr.ChatInterface(fn=respond,type="messages",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,4 unmodified lines],)with gr.Blocks() as demo:with gr.Sidebar():gr.Markdown("### Account")gr.LoginButton()chatbot.render()if __name__ == "__main__":demo.launch(server_name="0.0.0.0",server_port=7860,ssr_mode=False,show_error=True,)
app.py+87-30