Spaces:
Build error
Build error
Download app.py from Yahiya/Python-test: direct link, hf CLI and curl.
- Browser
- Download file 2.1 kB
-
https://huggingface.co/spaces/Yahiya/Python-test/resolve/7cd205691ee4e1405b8cf83d096000dbd2934ca4/app.py
- Command line
-
hf download hf://spaces/Yahiya/Python-test@7cd205691ee4e1405b8cf83d096000dbd2934ca4/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Yahiya/Python-test/resolve/7cd205691ee4e1405b8cf83d096000dbd2934ca4/app.py
2.1 kB
| import streamlit as st | |
| import torch | |
| from transformers import AutoTokenizer, AutoModelForCausalLM, TextGenerationPipeline | |
| import json | |
| import os | |
| # Use Gemma 2B model from HuggingFace | |
| def load_gemma(): | |
| model_id = "google/gemma-2b" | |
| tokenizer = AutoTokenizer.from_pretrained(model_id) | |
| model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.float32) # use float16 if on GPU | |
| pipe = TextGenerationPipeline(model=model, tokenizer=tokenizer, device=0 if torch.cuda.is_available() else -1) | |
| return pipe | |
| generator = load_gemma() | |
| # Data storage | |
| DATA_FILE = "user_data.json" | |
| def load_data(): | |
| if os.path.exists(DATA_FILE): | |
| with open(DATA_FILE, "r") as f: | |
| return json.load(f) | |
| return [] | |
| def save_data(data): | |
| with open(DATA_FILE, "w") as f: | |
| json.dump(data, f, indent=2) | |
| def generate_question(difficulty): | |
| prompt = f"Generate a {difficulty} level Python coding test question:\n" | |
| output = generator(prompt, max_new_tokens=100, do_sample=True)[0]['generated_text'] | |
| return output.strip().split("\n")[0] | |
| # Streamlit UI | |
| st.set_page_config(page_title="Python Test Generator", page_icon="๐") | |
| st.title("๐ Python Test Generator (Gemma)") | |
| st.markdown("Powered by Google's Gemma LLM") | |
| name = st.text_input("Enter your name") | |
| difficulty = st.selectbox("Choose difficulty level", ["easy", "medium", "hard"]) | |
| if st.button("Generate Question"): | |
| if not name: | |
| st.warning("Please enter your name.") | |
| else: | |
| question = generate_question(difficulty) | |
| st.session_state["question"] = question | |
| if "question" in st.session_state: | |
| st.subheader("Generated Question") | |
| st.write(st.session_state["question"]) | |
| answer = st.text_area("Your Answer") | |
| if st.button("Submit Answer"): | |
| entry = { | |
| "name": name, | |
| "difficulty": difficulty, | |
| "question": st.session_state["question"], | |
| "answer": answer | |
| } | |
| data = load_data() | |
| data.append(entry) | |
| save_data(data) | |
| st.success("Your answer has been saved!") | |