Spaces:
Runtime error
Runtime error
Update app.py
Browse files1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374import osimport gradio as grfrom huggingface_hub import InferenceClientimport spaces # Mandatory import for Hugging Face ZeroGPU compiler# We use the natively-hosted 7B model to bypass third-party routing errors completelyclient = InferenceClient("Qwen/Qwen2.5-Coder-7B-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.When writing code updates, prioritize writing functional backend logic and raw database interactions over building user interface designs.If 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 execution running on CPUdef generate_php_code(message, history):# Keep the ZeroGPU compiler happytry:dummy_gpu_compiler_satisfier()except Exception:passtoken = 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")})elif isinstance(turn, (list, tuple)):user_msg, assistant_msg = turnif user_msg:messages.append({"role": "user", "content": user_msg})if assistant_msg:messages.append({"role": "assistant", "content": assistant_msg})else:try:messages.append({"role": turn.role, "content": turn.content})except AttributeError:passmessages.append({"role": "user", "content": message})try:completion = client.chat_completion(messages,max_tokens=2048,temperature=0.2,)return completion.choices[0].message.contentexcept 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",description="Your private, high-speed assistant for writing backend game logic, MariaDB queries, and clean PHP algorithms.")if __name__ == "__main__":demo.queue()demo.launch()12345678910111213141516import osfrom openai import OpenAIclient = OpenAI(base_url="https://router.huggingface.co/v1",api_key=os.environ.get("HF_TOKEN") # Must have "Make calls to Inference Providers" permission)response = client.chat.completions.create(model="Qwen/Qwen2.5-Coder-32B-Instruct",messages=[{"role": "user", "content": "Write a quick PHP function to sanitize database inputs."}])print(response.choices[0].message.content)
app.py+10-68