Spaces:
Paused
Paused
Download app.py from sagar1697/test_space: direct link, hf CLI and curl.
- Browser
- Download file 1.58 kB
-
https://huggingface.co/spaces/sagar1697/test_space/resolve/main/app.py
- Command line
-
hf download hf://spaces/sagar1697/test_space/app.py
-
curl -L -o app.py https://huggingface.co/spaces/sagar1697/test_space/resolve/main/app.py
1.58 kB
| import os | |
| import gradio as gr | |
| import torch | |
| from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline | |
| # Correct model ID is HuggingFaceTB/SmolLM3-3B | |
| MODEL_ID = "HuggingFaceTB/SmolLM3-3B" | |
| print(f"Loading {MODEL_ID}...") | |
| tokenizer = AutoTokenizer.from_pretrained(MODEL_ID) | |
| model = AutoModelForCausalLM.from_pretrained( | |
| MODEL_ID, | |
| torch_dtype=torch.float16, | |
| device_map="cpu", | |
| low_cpu_mem_usage=True, | |
| ) | |
| print("Model loaded!") | |
| pipe = pipeline( | |
| "text-generation", | |
| model=model, | |
| tokenizer=tokenizer, | |
| max_new_tokens=512, | |
| temperature=0.7, | |
| do_sample=True, | |
| pad_token_id=tokenizer.eos_token_id, | |
| ) | |
| def chat_fn(message, history): | |
| messages = [] | |
| for human, assistant in history: | |
| messages.append({"role": "user", "content": human}) | |
| messages.append({"role": "assistant", "content": assistant}) | |
| messages.append({"role": "user", "content": message}) | |
| prompt = tokenizer.apply_chat_template( | |
| messages, | |
| tokenize=False, | |
| add_generation_prompt=True | |
| ) | |
| outputs = pipe(prompt, return_full_text=False) | |
| response = outputs[0]["generated_text"] | |
| if prompt in response: | |
| response = response[len(prompt):] | |
| return response | |
| demo = gr.ChatInterface( | |
| fn=chat_fn, | |
| title="SmolLM3-3B Chat (CPU)", | |
| description="Running Hugging Face's SmolLM3-3B on Free CPU Tier Space", | |
| textbox=gr.Textbox(placeholder="Type your message here...", container=False, scale=7), | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch(server_name="0.0.0.0", server_port=7860) |