Spaces:
Sleeping
Sleeping
Download app.py from arshhhhh12/tinyllama-doubt-ai: direct link, hf CLI and curl.
- Browser
- Download file 934 Bytes
-
https://huggingface.co/spaces/arshhhhh12/tinyllama-doubt-ai/resolve/main/app.py
- Command line
-
hf download hf://spaces/arshhhhh12/tinyllama-doubt-ai/app.py
-
curl -L -o app.py https://huggingface.co/spaces/arshhhhh12/tinyllama-doubt-ai/resolve/main/app.py
934 Bytes
| import gradio as gr | |
| from transformers import AutoModelForCausalLM, AutoTokenizer | |
| import torch | |
| # Load DistilGPT-2 model | |
| model_name = "distilgpt2" | |
| tokenizer = AutoTokenizer.from_pretrained(model_name) | |
| model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float32) | |
| # Function to generate AI responses | |
| def ask_doubt(question): | |
| input_text = f"Question: {question}\nAnswer:" | |
| input_ids = tokenizer(input_text, return_tensors="pt").input_ids | |
| output_ids = model.generate(input_ids, max_new_tokens=50, temperature=0.7) | |
| response = tokenizer.decode(output_ids[0], skip_special_tokens=True) | |
| return response | |
| # Create Gradio UI | |
| iface = gr.Interface( | |
| fn=ask_doubt, | |
| inputs=gr.Textbox(label="Enter Your Question"), | |
| outputs=gr.Textbox(label="AI Answer"), | |
| title="DistilGPT-2 Doubt Solver", | |
| description="Ask any academic question and get an answer instantly!" | |
| ) | |
| # Launch app | |
| iface.launch() | |