Download app.py from ghost0912/clawdbot: direct link, hf CLI and curl.
- Browser
- Download file 1.09 kB
-
https://huggingface.co/spaces/ghost0912/clawdbot/resolve/main/app.py
- Command line
-
hf download hf://spaces/ghost0912/clawdbot/app.py
-
curl -L -o app.py https://huggingface.co/spaces/ghost0912/clawdbot/resolve/main/app.py
1.09 kB
| import gradio as gr | |
| from transformers import AutoModelForCausalLM, AutoTokenizer | |
| # Load pre-trained DialoGPT model from Hugging Face | |
| model_name = "microsoft/DialoGPT-medium" # You can replace this with other models like GPT-2 if needed | |
| tokenizer = AutoTokenizer.from_pretrained(model_name) | |
| model = AutoModelForCausalLM.from_pretrained(model_name) | |
| # Function to generate chatbot responses | |
| def chatbot_response(input_text): | |
| # Encode the input text and add special tokens for the model | |
| input_ids = tokenizer.encode(input_text + tokenizer.eos_token, return_tensors='pt') | |
| # Generate a response | |
| chat_history_ids = model.generate(input_ids, max_length=1000, pad_token_id=tokenizer.eos_token_id, no_repeat_ngram_size=2) | |
| # Decode the response to text | |
| response = tokenizer.decode(chat_history_ids[:, input_ids.shape[-1]:][0], skip_special_tokens=True) | |
| return response | |
| # Create a Gradio interface to take user input and output a chatbot response | |
| interface = gr.Interface(fn=chatbot_response, inputs="text", outputs="text") | |
| # Launch the app | |
| interface.launch() | |