Spaces:
Build error
Build error
added main file
Browse files
main.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
import json
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
# Initialize HuggingFace model (text generation)
|
| 7 |
+
@st.cache_resource
|
| 8 |
+
def load_generator():
|
| 9 |
+
return pipeline("text-generation", model="gpt2")
|
| 10 |
+
|
| 11 |
+
generator = load_generator()
|
| 12 |
+
|
| 13 |
+
# JSON file to store user data
|
| 14 |
+
DATA_FILE = "user_data.json"
|
| 15 |
+
|
| 16 |
+
def load_data():
|
| 17 |
+
if os.path.exists(DATA_FILE):
|
| 18 |
+
with open(DATA_FILE, "r") as f:
|
| 19 |
+
return json.load(f)
|
| 20 |
+
return []
|
| 21 |
+
|
| 22 |
+
def save_data(data):
|
| 23 |
+
with open(DATA_FILE, "w") as f:
|
| 24 |
+
json.dump(data, f, indent=2)
|
| 25 |
+
|
| 26 |
+
def generate_python_question(difficulty):
|
| 27 |
+
prompt = f"Generate a {difficulty} level Python coding test question."
|
| 28 |
+
output = generator(prompt, max_length=100, num_return_sequences=1)[0]["generated_text"]
|
| 29 |
+
return output.split("\n")[0].strip() # Get only the first line
|
| 30 |
+
|
| 31 |
+
# Streamlit UI
|
| 32 |
+
st.title("🧠 Python Test Generator with HuggingFace")
|
| 33 |
+
st.markdown("Generate Python test questions and save user data")
|
| 34 |
+
|
| 35 |
+
name = st.text_input("Enter your name")
|
| 36 |
+
difficulty = st.selectbox("Choose difficulty level", ["easy", "medium", "hard"])
|
| 37 |
+
|
| 38 |
+
if st.button("Generate Question"):
|
| 39 |
+
if not name:
|
| 40 |
+
st.warning("Please enter your name.")
|
| 41 |
+
else:
|
| 42 |
+
question = generate_python_question(difficulty)
|
| 43 |
+
st.session_state["question"] = question
|
| 44 |
+
|
| 45 |
+
# Display generated question
|
| 46 |
+
if "question" in st.session_state:
|
| 47 |
+
st.subheader("Your Python Question:")
|
| 48 |
+
st.write(st.session_state["question"])
|
| 49 |
+
|
| 50 |
+
answer = st.text_area("Your Answer")
|
| 51 |
+
if st.button("Submit Answer"):
|
| 52 |
+
user_entry = {
|
| 53 |
+
"name": name,
|
| 54 |
+
"difficulty": difficulty,
|
| 55 |
+
"question": st.session_state["question"],
|
| 56 |
+
"answer": answer
|
| 57 |
+
}
|
| 58 |
+
data = load_data()
|
| 59 |
+
data.append(user_entry)
|
| 60 |
+
save_data(data)
|
| 61 |
+
st.success("Answer submitted and saved!")
|