Spaces:
Runtime error
Runtime error
Download app.py from daniyal-10/StudyHelperAI: direct link, hf CLI and curl.
- Browser
- Download file 1.59 kB
-
https://huggingface.co/spaces/daniyal-10/StudyHelperAI/resolve/main/app.py
- Command line
-
hf download hf://spaces/daniyal-10/StudyHelperAI/app.py
-
curl -L -o app.py https://huggingface.co/spaces/daniyal-10/StudyHelperAI/resolve/main/app.py
1.59 kB
| import gradio as gr | |
| from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline | |
| import torch | |
| # --- Load the AI Model --- | |
| model_name = "TheBloke/wizardLM-7B-HF" | |
| tokenizer = AutoTokenizer.from_pretrained(model_name) | |
| model = AutoModelForCausalLM.from_pretrained( | |
| model_name, | |
| device_map="auto", # Uses GPU if available | |
| torch_dtype=torch.float16 # Uses half-precision for speed | |
| ) | |
| chatbot = pipeline("text-generation", model=model, tokenizer=tokenizer, max_new_tokens=200) | |
| # --- Conversation Memory --- | |
| history = [] | |
| def respond(user_input): | |
| if not user_input.strip(): | |
| return "" | |
| history.append(f"You: {user_input}") | |
| prompt = "\n".join(history) | |
| response = chatbot(prompt)[0]['generated_text'] | |
| history.append(f"AI: {response}") | |
| # Format chat history | |
| chat_text = "" | |
| for msg in history: | |
| if msg.startswith("You:"): | |
| chat_text += f"<p style='color:blue;'><b>{msg}</b></p>" | |
| else: | |
| chat_text += f"<p style='color:green;'><b>{msg}</b></p>" | |
| return chat_text | |
| def clear_chat(): | |
| history.clear() | |
| return "" | |
| # --- Gradio Interface --- | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# Study Helper AI") | |
| gr.Markdown("Ask me anything! Model: WizardLM-7B-HF (Open Access).") | |
| chatbox = gr.HTML() | |
| user_input = gr.Textbox(lines=2, placeholder="Type your question here...") | |
| send_btn = gr.Button("Send") | |
| clear_btn = gr.Button("Clear Chat") | |
| send_btn.click(respond, inputs=user_input, outputs=chatbox) | |
| clear_btn.click(clear_chat, outputs=chatbox) | |
| demo.launch() | |