import gradio as gr import requests # Hugging Face Inference API URL API_URL = "https://api-inference.huggingface.co/models/Polygl0t/Tucano2-qwen-3.7B-Instruct" # NOTE: Apne Hugging Face Settings -> Access Tokens mein jaakar ek READ token banayein # Aur Space ke Settings mein 'HF_TOKEN' naam ke Repository Secret mein use daal dein. # Agar bina token ke chalana hai, toh kabhi-kabhi rate limit aa sakti hai. headers = {"Authorization": "Bearer YOUR_HF_TOKEN_HERE"} def chat_with_model(user_message): payload = { "inputs": f"<|im_start|>user\n{user_message}<|im_end|>\n<|im_start||>assistant\n", "parameters": {"max_new_tokens": 100, "return_full_text": False} } try: response = requests.post(API_URL, headers=headers, json=payload) output = response.json() # Jawab nikalne ke liye if isinstance(output, list) and len(output) > 0: return output[0].get('generated_text', 'Koi jawab nahi mila.') elif isinstance(output, dict) and 'error' in output: return f"Error: {output['error']}" return str(output) except Exception as e: return f"Kuch gadbad hui: {str(e)}" # Gradio UI demo = gr.Interface( fn=chat_with_model, inputs=gr.Textbox(label="Aapka Sawal", placeholder="Yahan likhein..."), outputs=gr.Textbox(label="Model ka Jawab"), title="Tucano2 Qwen Chat (Fast API)" ) if __name__ == "__main__": demo.launch()