Download app.py from freegpu/chatbot: direct link, hf CLI and curl.
- Browser
- Download file 953 Bytes
-
https://huggingface.co/spaces/freegpu/chatbot/resolve/main/app.py
- Command line
-
hf download hf://spaces/freegpu/chatbot/app.py
-
curl -L -o app.py https://huggingface.co/spaces/freegpu/chatbot/resolve/main/app.py
953 Bytes
| import gradio as gr | |
| from transformers import GPT2LMHeadModel, GPT2Tokenizer | |
| import torch | |
| # Load model from HF Hub | |
| model_name = "freegpu/kryptonite_chat" | |
| tokenizer = GPT2Tokenizer.from_pretrained(model_name) | |
| model = GPT2LMHeadModel.from_pretrained(model_name) | |
| model.eval() | |
| def chat_with_bot(user_input): | |
| input_text = f"Q: {user_input} A:" | |
| inputs = tokenizer.encode(input_text, return_tensors="pt") | |
| with torch.no_grad(): | |
| outputs = model.generate( | |
| inputs, | |
| max_length=150, | |
| do_sample=True, | |
| top_p=0.95, | |
| top_k=50, | |
| temperature=0.7 | |
| ) | |
| text = tokenizer.decode(outputs[0], skip_special_tokens=True) | |
| answer = text.split("A:")[-1].strip() | |
| return answer | |
| iface = gr.Interface( | |
| fn=chat_with_bot, | |
| inputs="text", | |
| outputs="text", | |
| title="KryptoniteWayne Chatbot", | |
| description="Ask me anything about games or my website!" | |
| ) | |
| iface.launch() | |