Spaces:
Runtime error
Runtime error
Download app.py from gampala1234/phi3mini: direct link, hf CLI and curl.
- Browser
- Download file 1.17 kB
-
https://huggingface.co/spaces/gampala1234/phi3mini/resolve/main/app.py
- Command line
-
hf download hf://spaces/gampala1234/phi3mini/app.py
-
curl -L -o app.py https://huggingface.co/spaces/gampala1234/phi3mini/resolve/main/app.py
1.17 kB
| import gradio as gr | |
| from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline | |
| model_id = "microsoft/phi-3-mini-4k-instruct" | |
| # Load tokenizer and model without device_map | |
| tokenizer = AutoTokenizer.from_pretrained(model_id) | |
| model = AutoModelForCausalLM.from_pretrained( | |
| model_id, | |
| trust_remote_code=True | |
| ) | |
| # Define text generation pipeline | |
| pipe = pipeline( | |
| "text-generation", | |
| model=model, | |
| tokenizer=tokenizer, | |
| max_new_tokens=512, | |
| temperature=0.7, | |
| do_sample=True, | |
| top_p=0.9, | |
| ) | |
| # Format input for the Phi-3 instruction format | |
| def generate_response(prompt): | |
| messages = [ | |
| {"role": "user", "content": prompt} | |
| ] | |
| prompt_text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True) | |
| output = pipe(prompt_text)[0]["generated_text"] | |
| return output[len(prompt_text):] | |
| # Gradio UI | |
| demo = gr.Interface( | |
| fn=generate_response, | |
| inputs=gr.Textbox(label="Your Prompt", placeholder="Ask something..."), | |
| outputs=gr.Textbox(label="Phi-3 Mini Response"), | |
| title="🧠 Phi-3 Mini (Microsoft)", | |
| description="Running Phi-3 Mini in Hugging Face CPU Space." | |
| ) | |
| demo.launch() | |