import gradio as gr import time from huggingface_inference_api import InferenceClient import os from huggingface_inference_api import InferenceClient HF_TOKEN = os.getenv("HF_API_KEY") # Ortam değişkeninden al client = InferenceClient(HF_API_KEY) MODELS = { "Flower Chat": "openai/gpt-oss-20b", "Flower Coder": "Qwen/Qwen3-Coder-30B-A3B-Instruct", "Flower MULTI": "moonshotai/Kimi-K2-Instruct", } def generate_response(messages, model_name): # messages = list of dicts {role, content} # model_name = seçilen model key response = client.chat_completion( model=MODELS[model_name], messages=messages, ) return response.choices[0].message.content def process_chat(user_message, chat_history, model_name, think_mode): # chat_history: list of tuples (user_msg, bot_msg) # think_mode: bool - düşünme animasyonu göster if chat_history is None: chat_history = [] # Convert chat_history tuples to messages format messages = [] for user_m, bot_m in chat_history: messages.append({"role": "user", "content": user_m}) messages.append({"role": "assistant", "content": bot_m}) # Append current user message messages.append({"role": "user", "content": user_message}) if think_mode: # Düşünme animasyonu için gecikmeli cevap (örnek) time.sleep(1.5) try: bot_reply = generate_response(messages, model_name) except Exception as e: bot_reply = f"❌ Hata: {str(e)}" chat_history.append((user_message, bot_reply)) return chat_history, chat_history # Kod modunda sadece Flower Coder aktif def run_code_mode(user_code, chat_history, model_name): if chat_history is None: chat_history = [] messages = [] for user_m, bot_m in chat_history: messages.append({"role": "user", "content": user_m}) messages.append({"role": "assistant", "content": bot_m}) messages.append({"role": "user", "content": user_code}) try: response = client.chat_completion( model=MODELS[model_name], messages=messages, ) bot_reply = response.choices[0].message.content except Exception as e: bot_reply = f"❌ Hata: {str(e)}" chat_history.append((user_code, bot_reply)) return bot_reply, chat_history with gr.Blocks(css=""" body { background: linear-gradient(135deg, #F5F5F5 0%, #FFF0F8 100%); font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, "Open Sans", "Helvetica Neue", sans-serif;} .chatbox .message.user { background-color: #ffceda !important; color: #000;} .chatbox .message.bot { background-color: #fff !important; color: #000;} .header { font-size: 2.5rem; font-weight: 700; color: #FFB6C1; text-align: center; margin-bottom: 10px;} .footer { margin-top: 10px;} .think-indicator { font-style: italic; color: gray; margin-left: 10px;} .model-dropdown { width: 180px;} """) as demo: gr.Markdown("