Download app.py from fubrix/test3: direct link, hf CLI and curl.
- Browser
- Download file 1.76 kB
-
https://huggingface.co/spaces/fubrix/test3/resolve/main/app.py
- Command line
-
hf download hf://spaces/fubrix/test3/app.py
-
curl -L -o app.py https://huggingface.co/spaces/fubrix/test3/resolve/main/app.py
1.76 kB
| import gradio as gr | |
| import torch | |
| from transformers import AutoProcessor, AutoModelForCausalLM | |
| print("Hello1") | |
| MODEL_ID = "google/gemma-4-E2B-it" | |
| # Load model | |
| processor = AutoProcessor.from_pretrained(MODEL_ID) | |
| model = AutoModelForCausalLM.from_pretrained( | |
| MODEL_ID, | |
| dtype="auto", | |
| device_map="auto" | |
| ) | |
| ''' | |
| # Prompt | |
| messages = [ | |
| {"role": "system", "content": "You are a helpful assistant."}, | |
| {"role": "user", "content": "Write a short joke about saving RAM."}, | |
| ] | |
| print("Hello3") | |
| # Process input | |
| text = processor.apply_chat_template( | |
| messages, | |
| tokenize=False, | |
| add_generation_prompt=True, | |
| enable_thinking=False | |
| ) | |
| inputs = processor(text=text, return_tensors="pt").to(model.device) | |
| input_len = inputs["input_ids"].shape[-1] | |
| print("Hello4") | |
| # Generate output | |
| outputs = model.generate(**inputs, max_new_tokens=1024) | |
| response = processor.decode(outputs[0][input_len:], skip_special_tokens=False) | |
| print("Hello5") | |
| # Parse output | |
| result = processor.parse_response(response) | |
| print(result) | |
| while True: | |
| pass | |
| ''' | |
| def predict(message, history): | |
| print("Hello2") | |
| print(message) | |
| print(history) | |
| print(model.device) | |
| messages = history + [{"role":"user","content":message}] | |
| input_text = processor.apply_chat_template(messages,tokenize=False,add_generation_prompt=True,enable_thinking=False) | |
| inputs = processor(text=input_text, return_tensors="pt").to(model.device) | |
| input_len = inputs["input_ids"].shape[-1] | |
| outputs = model.generate(**inputs, max_new_tokens=1024) | |
| response = processor.decode(outputs[0][input_len:], skip_special_tokens=True) | |
| result = processor.parse_response(response) | |
| print("Hello3") | |
| return result | |
| demo = gr.ChatInterface(predict, api_name="chat") | |
| demo.launch() |