Spaces:
Sleeping
Sleeping
Download app.py from tabi2000222/StudyflowAI: direct link, hf CLI and curl.
- Browser
- Download file 1.09 kB
-
https://huggingface.co/spaces/tabi2000222/StudyflowAI/resolve/main/app.py
- Command line
-
hf download hf://spaces/tabi2000222/StudyflowAI/app.py
-
curl -L -o app.py https://huggingface.co/spaces/tabi2000222/StudyflowAI/resolve/main/app.py
1.09 kB
| import gradio as gr | |
| from transformers import AutoModelForCausalLM, AutoTokenizer, TextStreamer | |
| import torch | |
| MODEL = "microsoft/Phi-3-mini-4k-instruct" | |
| tokenizer = AutoTokenizer.from_pretrained(MODEL) | |
| model = AutoModelForCausalLM.from_pretrained( | |
| MODEL, | |
| torch_dtype=torch.float16, | |
| device_map="auto" | |
| ) | |
| def respond(message, history): | |
| history_text = "" | |
| for user, bot in history: | |
| history_text += f"User: {user}\nAssistant: {bot}\n" | |
| prompt = history_text + f"User: {message}\nAssistant:" | |
| inputs = tokenizer(prompt, return_tensors="pt").to(model.device) | |
| streamer = TextStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True) | |
| output = model.generate( | |
| **inputs, | |
| max_new_tokens=300, | |
| temperature=0.7, | |
| top_p=0.9, | |
| streamer=streamer | |
| ) | |
| output_text = tokenizer.decode(output[0], skip_special_tokens=True) | |
| return output_text.split("Assistant:")[-1].strip() | |
| chatbot = gr.ChatInterface( | |
| respond, | |
| title="Free Phi-3 Chatbot", | |
| description="100% free, no token required." | |
| ) | |
| chatbot.launch() | |