import gradio as gr import torch # Global variable for model model = None def load_model(): global model try: # APNI .pth FILE KA NAAM YAHAN LIKHEIN (jaise "my_model.pth") model_path = "model.pth" # Try loading the complete model model = torch.load(model_path, map_location=torch.device('cpu')) model.eval() print("Model kamyabi se load ho gaya!") except Exception as e: print(f"Model load hone mein masla aaya: {e}") def predict(input_text): if model is None: return "Error: Model theek se load nahi hua. Spaces ke 'Logs' check karein." try: # ==================================================================== # IMPORTANT: Text Preprocessing (Text ko numbers mein convert karna) # ==================================================================== # PyTorch model ko text ki jagah Tensors chahiye hotay hain. # Aapne text ko jis tareeqe se train karte waqt numbers mein badla tha # (jaise Tokenizer, ya TF-IDF), woh logic yahan aayegi. # Misaal ke tor par (Dummy Tensor) - Isko apne model ke hisaab se change karein: # tokenized_input = torch.zeros(1, 10, dtype=torch.long) # output = model(tokenized_input) # result = output.argmax(dim=1).item() # Abhi ke liye hum sirf ek confirmation message return kar rahay hain: return f"Aapka input '{input_text}' receive ho gaya hai.\n(Model se asal jawab lene ke liye line 26 par tokenization/preprocessing add karein)" except Exception as e: return f"Prediction Error: {e}" # Start karte hi model load karo load_model() # Gradio Interface (UI) iface = gr.Interface( fn=predict, inputs=gr.Textbox(lines=2, placeholder="Apni .txt file ka koi sentence yahan likhein..."), outputs="text", title="Mera PyTorch Model", description="Apna text input karein aur PyTorch model se output dekhein." ) iface.launch()