Spaces:
Runtime error
Runtime error
Download app.py from Chuxia-sys/T5-space: direct link, hf CLI and curl.
- Browser
- Download file 1.19 kB
-
https://huggingface.co/spaces/Chuxia-sys/T5-space/resolve/main/app.py
- Command line
-
hf download hf://spaces/Chuxia-sys/T5-space/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Chuxia-sys/T5-space/resolve/main/app.py
1.19 kB
| import gradio as gr | |
| from transformers import AutoTokenizer, AutoModelForSeq2SeqLM | |
| import torch | |
| # ========================= | |
| # LOAD MODEL | |
| # ========================= | |
| MODEL_NAME = "Chuxia-sys/travel-chatbot-flan-t5" # πΉ Replace with your HF repo ID | |
| print("π Loading model...") | |
| tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME) | |
| model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_NAME) | |
| print("β Model loaded!\n") | |
| # ========================= | |
| # CHAT FUNCTION | |
| # ========================= | |
| def chat_with_bot(message, history): # β must accept two arguments | |
| if not message.strip(): | |
| return "Please enter a message π" | |
| inputs = tokenizer(message, return_tensors="pt") | |
| outputs = model.generate( | |
| **inputs, | |
| max_new_tokens=300, | |
| do_sample=True, | |
| temperature=0.7, | |
| top_p=0.9, | |
| repetition_penalty=1.8, | |
| no_repeat_ngram_size=3, | |
| ) | |
| response = tokenizer.decode(outputs[0], skip_special_tokens=True) | |
| # Clean up duplicate lines | |
| lines = [] | |
| for line in response.split("\n"): | |
| if line.strip() and line not in lines: | |
| lines.append(line) | |
| response = "\n".join(lines) | |
| return response |