Spaces:
Sleeping
Sleeping
Update app.py
Browse files1234567891011121314151617181920212223242526272829301 unmodified line32333435363738394041424344454647import gradio as grfrom huggingface_hub import InferenceClient# We use the 32B Coder model which is generally available on the free APImodel_id = "Qwen/Qwen2.5-Coder-32B-Instruct"def respond(message, history):# Initialize the client inside the function to handle sessions correctlyclient = InferenceClient(model_id)# Build the message history for the APImessages = []for user_msg, bot_msg in history:messages.append({"role": "user", "content": user_msg})messages.append({"role": "assistant", "content": bot_msg})messages.append({"role": "user", "content": message})# Generate the responseresponse_text = ""try:# Stream the responsestream = client.chat_completion(messages,max_tokens=2048,stream=True,temperature=0.7)for chunk in stream:content = chunk.choices[0].delta.content1 unmodified lineresponse_text += contentyield response_textexcept Exception as e:yield f"Error: {str(e)}. The model might be busy or too large for the current free tier."# Build the UIwith gr.Blocks(fill_height=True) as demo:with gr.Sidebar():gr.Markdown("# AI Coding Assistant")gr.Markdown(f"Running **{model_id}**")gr.Markdown("If you see an error, the free API might be overloaded. Try again in a minute.")gr.LoginButton("Sign in")gr.ChatInterface(respond)demo.launch()1234567891011121314151617181920212223242526272829303132331 unmodified line353637383940414243444546474849505152535455565758596061626364656667import gradio as grfrom huggingface_hub import InferenceClient# Using Qwen 2.5 Codermodel_id = "Qwen/Qwen2.5-Coder-32B-Instruct"def respond(message, history, system_message, temperature):# Initialize the clientclient = InferenceClient(model_id)# 1. Start with the System Messagemessages = [{"role": "system", "content": system_message}]# 2. Add the historyfor user_msg, bot_msg in history:messages.append({"role": "user", "content": user_msg})messages.append({"role": "assistant", "content": bot_msg})# 3. Add the current user messagemessages.append({"role": "user", "content": message})# Generate responseresponse_text = ""try:stream = client.chat_completion(messages,max_tokens=2048,stream=True,temperature=temperature,top_p=0.9)for chunk in stream:content = chunk.choices[0].delta.content1 unmodified lineresponse_text += contentyield response_textexcept Exception as e:yield f"Error: {str(e)}. The model might be busy."# Build the UIwith gr.Blocks(fill_height=True) as demo:with gr.Sidebar():gr.Markdown("# AI Coding Assistant")gr.Markdown(f"Running **{model_id}**")gr.LoginButton("Sign in")gr.ChatInterface(respond,additional_inputs=[# System Message Inputgr.Textbox(value="You are a helpful assistant.",label="System Instruction",lines=2),# Temperature Slidergr.Slider(minimum=0.1,maximum=2.0,value=0.7,step=0.1,label="Temperature")])demo.launch()
app.py+31-11