Download app.py from Misarthak/NSTGPT: direct link, hf CLI and curl.
- Browser
- Download file 1.03 kB
-
https://huggingface.co/spaces/Misarthak/NSTGPT/resolve/main/app.py
- Command line
-
hf download hf://spaces/Misarthak/NSTGPT/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Misarthak/NSTGPT/resolve/main/app.py
1.03 kB
| import gradio as gr | |
| from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline | |
| MODEL_NAME = "microsoft/phi-2" | |
| tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME) | |
| model = AutoModelForCausalLM.from_pretrained(MODEL_NAME) | |
| pipe = pipeline("text-generation", model=model, tokenizer=tokenizer, max_new_tokens=250) | |
| def chat(message, history): | |
| history_text = "".join(f"User: {u}\nAssistant: {a}\n" for u, a in history) | |
| prompt = f"{history_text}User: {message}\nAssistant:" | |
| output = pipe(prompt)[0]["generated_text"] | |
| answer = output.split("Assistant:")[-1].strip() | |
| history.append((message, answer)) | |
| return "", history | |
| with gr.Blocks() as demo: | |
| gr.Markdown("## 🤖 Fast Phi-2 Chatbot (Free CPU Space)") | |
| chatbot = gr.Chatbot() | |
| msg = gr.Textbox(label="Your message...") | |
| clear = gr.Button("Clear Chat") | |
| msg.submit(chat, [msg, chatbot], [msg, chatbot]) | |
| clear.click(lambda: None, None, chatbot, queue=False) | |
| demo.launch(server_name="0.0.0.0", server_port=7860, share=True) |