Update app.py
Browse files62 unmodified lines636465666768697014 unmodified lines85868788899091929394959697989910010110210310410510610710810911011111211311411511611711862 unmodified lines"""def respond(message: str, history_json: str, system_message: str, max_tokens: int, temperature: float, top_p: float) -> str:"""Главная функция API."""try:history = json.loads(history_json) if history_json else []except Exception:14 unmodified linestop_p=float(top_p),):if len(msg.choices) and msg.choices[0].delta.content:response += msg.choices[0].delta.contentreturn responseexcept Exception as e:return f"<response>⚠️ Ошибка API Hugging Face: {str(e)}</response>"with gr.Blocks(title="AI Coder Agent Backend") as demo:gr.Markdown("# 🤖 AI Coder Agent Backend")gr.Markdown("API для локального CLI-агента")with gr.Row():message = gr.Textbox(label="Message", lines=3)history_json = gr.Textbox(label="History JSON", value="[]", lines=2)system_message = gr.Textbox(label="System prompt", value=SYSTEM_PROMPT, lines=10)max_tokens = gr.Slider(minimum=1, maximum=8192, value=4096, step=1, label="Max new tokens")temperature = gr.Slider(minimum=0.1, maximum=2.0, value=0.2, step=0.1, label="Temperature")top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p")output = gr.Textbox(label="Response", lines=20)submit_btn = gr.Button("Send")submit_btn.click(fn=respond,inputs=[message, history_json, system_message, max_tokens, temperature, top_p],outputs=output,api_name="/respond")if __name__ == "__main__":demo.launch()62 unmodified lines636465666768697014 unmodified lines85868788899091929394959697989910010110210310410510610710810911011111262 unmodified lines"""def respond(message: str, history_json: str, system_message: str, max_tokens: int, temperature: float, top_p: float):"""Генератор - стримит ответ токен за токеном."""try:history = json.loads(history_json) if history_json else []except Exception:14 unmodified linestop_p=float(top_p),):if len(msg.choices) and msg.choices[0].delta.content:token = msg.choices[0].delta.contentresponse += token# Yield текущий полный ответ (Gradio обновляет output)yield responseexcept Exception as e:yield f"<response>Ошибка API Hugging Face: {str(e)}</response>"demo = gr.Interface(fn=respond,inputs=[gr.Textbox(label="Message", lines=3),gr.Textbox(label="History JSON", value="[]", lines=2),gr.Textbox(label="System prompt", value=SYSTEM_PROMPT, lines=10),gr.Slider(minimum=1, maximum=8192, value=4096, step=1, label="Max new tokens"),gr.Slider(minimum=0.1, maximum=2.0, value=0.2, step=0.1, label="Temperature"),gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p"),],outputs=gr.Textbox(label="Response", lines=20),title="AI Coder Agent Backend",description="Streaming API для локального CLI-агента",)if __name__ == "__main__":demo.launch()
app.py+23-29