Spaces:
Runtime error
Runtime error
Update app.py
Browse files1234567891 unmodified line11121314151617181920212223242526272829303132333412 unmodified lines474849505152533 unmodified lines57585960616263import gradio as grfrom huggingface_hub import InferenceClientimport spaces # Mandatory import for Hugging Face ZeroGPU compiler# Initialize the clientclient = InferenceClient("Qwen/Qwen2.5-Coder-32B-Instruct")SYSTEM_PROMPT = """You are a senior PHP and MariaDB software engineer specializing in backend game development.Your goal is to write highly optimized, clean, and modern procedural or object-oriented PHP algorithms.1 unmodified lineIf database updates are required, prioritize writing raw console queries or standard PDO commands.Keep explanations concise, directly addressing logic, database structures, and performance optimization. Avoid generic conversational fluff."""# 1. This dummy function exists ONLY to satisfy the ZeroGPU compiler during startup.# It prevents the "No @spaces.GPU function detected" error and uses almost zero resources.@spaces.GPU(duration=1)def dummy_gpu_compiler_satisfier():return "ZeroGPU check passed"# 2. The actual API execution runs safely on the CPU.# This completely avoids all ZeroGPU pickling, serialization, and ValueError bugs!def generate_php_code(message, history):# Run the dummy function just to ensure the GPU decorator is evaluatedtry:dummy_gpu_compiler_satisfier()except Exception:passmessages = [{"role": "system", "content": SYSTEM_PROMPT}]# Process prior chat turns safelyfor turn in history:if isinstance(turn, dict):messages.append({"role": turn.get("role"), "content": turn.get("content")})12 unmodified linesmessages.append({"role": "user", "content": message})try:# Request completion directly on the CPUcompletion = client.chat_completion(messages,max_tokens=2048,3 unmodified linesexcept Exception as e:return f"Error: {str(e)}\n\nIf this persists, please try sending your prompt again."# 3. Clean Gradio Interfacedemo = gr.ChatInterface(fn=generate_php_code,title="๐ PHP Algorithm Specialist",123456789101112131 unmodified line15161718192021222324252627282930313233343536373839404112 unmodified lines545556575859603 unmodified lines64656667686970import osimport gradio as grfrom huggingface_hub import InferenceClientimport spaces # Mandatory import for Hugging Face ZeroGPU compiler# Initialize the client, fetching your HF_TOKEN secret securelyclient = InferenceClient("Qwen/Qwen2.5-Coder-32B-Instruct",token=os.environ.get("HF_TOKEN"))SYSTEM_PROMPT = """You are a senior PHP and MariaDB software engineer specializing in backend game development.Your goal is to write highly optimized, clean, and modern procedural or object-oriented PHP algorithms.1 unmodified lineIf database updates are required, prioritize writing raw console queries or standard PDO commands.Keep explanations concise, directly addressing logic, database structures, and performance optimization. Avoid generic conversational fluff."""# 1. Dummy GPU function to satisfy ZeroGPU compiler requirements@spaces.GPU(duration=1)def dummy_gpu_compiler_satisfier():return "ZeroGPU check passed"# 2. History assembly and API request execution running on CPUdef generate_php_code(message, history):# Ensure the dummy decorator compiles correctly on ZeroGPUtry:dummy_gpu_compiler_satisfier()except Exception:pass# Double-check that the Space successfully picked up your environment tokentoken = os.environ.get("HF_TOKEN")if not token:return "Error: HF_TOKEN is missing. Please add it to your Space's 'Settings -> Variables and secrets' as a secret."messages = [{"role": "system", "content": SYSTEM_PROMPT}]# Safely parse Gradio's chat historyfor turn in history:if isinstance(turn, dict):messages.append({"role": turn.get("role"), "content": turn.get("content")})12 unmodified linesmessages.append({"role": "user", "content": message})try:# Request final code generationcompletion = client.chat_completion(messages,max_tokens=2048,3 unmodified linesexcept Exception as e:return f"Error: {str(e)}\n\nIf this persists, please try sending your prompt again."# 3. Clean Gradio layoutdemo = gr.ChatInterface(fn=generate_php_code,title="๐ PHP Algorithm Specialist",
app.py+17-10