Spaces:
Sleeping
Sleeping
Download app.py from studentsonug/autocaption: direct link, hf CLI and curl.
- Browser
- Download file 1.13 kB
-
https://huggingface.co/spaces/studentsonug/autocaption/resolve/main/app.py
- Command line
-
hf download hf://spaces/studentsonug/autocaption/app.py
-
curl -L -o app.py https://huggingface.co/spaces/studentsonug/autocaption/resolve/main/app.py
1.13 kB
| import gradio as gr | |
| import torch | |
| from transformers import AutoModelForSeq2SeqLM, AutoTokenizer | |
| # Model aur tokenizer load karo | |
| model_name = "google/flan-t5-base" | |
| tokenizer = AutoTokenizer.from_pretrained(model_name) | |
| # Auto GPU detect | |
| device = "cuda" if torch.cuda.is_available() else "cpu" | |
| print(f"🚀 Using device: {device}") | |
| model = AutoModelForSeq2SeqLM.from_pretrained(model_name).to(device) | |
| def generate_text(prompt): | |
| inputs = tokenizer(prompt, return_tensors="pt", padding=True, truncation=True).to(device) | |
| outputs = model.generate( | |
| **inputs, | |
| max_new_tokens=200, | |
| temperature=0.7, | |
| top_p=0.9 | |
| ) | |
| return tokenizer.decode(outputs[0], skip_special_tokens=True) | |
| # Gradio UI | |
| demo = gr.Interface( | |
| fn=generate_text, | |
| inputs=gr.Textbox(lines=3, placeholder="Enter your prompt here..."), | |
| outputs="text", | |
| title="FLAN-T5 Text Generation (Auto GPU/CPU)", | |
| description="Google FLAN-T5-Base deployed on Hugging Face Spaces. Runs on GPU if available, else falls back to CPU." | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch(server_name="0.0.0.0", server_port=7860, share=True) | |