Spaces:
Runtime error
Runtime error
| import torch | |
| import gradio as gr | |
| # Load your trained Apple model | |
| checkpoint = torch.load("apple_model.pth") | |
| model = YourModelClass(len(checkpoint["word2idx"])) # replace with your class | |
| model.load_state_dict(checkpoint["model_state"]) | |
| word2idx = checkpoint["word2idx"] | |
| idx2word = checkpoint["idx2word"] | |
| # Tokenizer & vectorizer | |
| def tokenize(text): | |
| return text.lower().split() | |
| def vectorize(sentence): | |
| vec = torch.zeros(len(word2idx)) | |
| for word in tokenize(sentence): | |
| if word in word2idx: | |
| vec[word2idx[word]] += 1 | |
| return vec | |
| # Chat function | |
| def chatbot(input_text): | |
| x = vectorize(input_text) | |
| output = model(x) | |
| top_words = torch.topk(output, 6).indices | |
| response = " ".join(idx2word[i.item()] for i in top_words) | |
| return response | |
| # Gradio Interface | |
| iface = gr.Interface( | |
| fn=chatbot, | |
| inputs=gr.Textbox(label="You"), | |
| outputs=gr.Textbox(label="Apple"), | |
| title="Apple Chatbot", | |
| description="Chat with Apple, your AI assistant!" | |
| ) | |
| iface.launch() | |