Yahiya commited on
Commit
7cd2056
·
verified ·
1 Parent(s): d7d9569

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -19
app.py CHANGED
@@ -1,16 +1,21 @@
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():
@@ -23,14 +28,15 @@ 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"])
@@ -39,23 +45,22 @@ 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!")
 
1
  import streamlit as st
2
+ import torch
3
+ from transformers import AutoTokenizer, AutoModelForCausalLM, TextGenerationPipeline
4
  import json
5
  import os
6
 
7
+ # Use Gemma 2B model from HuggingFace
8
  @st.cache_resource
9
+ def load_gemma():
10
+ model_id = "google/gemma-2b"
11
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
12
+ model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.float32) # use float16 if on GPU
13
+ pipe = TextGenerationPipeline(model=model, tokenizer=tokenizer, device=0 if torch.cuda.is_available() else -1)
14
+ return pipe
15
 
16
+ generator = load_gemma()
17
 
18
+ # Data storage
19
  DATA_FILE = "user_data.json"
20
 
21
  def load_data():
 
28
  with open(DATA_FILE, "w") as f:
29
  json.dump(data, f, indent=2)
30
 
31
+ def generate_question(difficulty):
32
+ prompt = f"Generate a {difficulty} level Python coding test question:\n"
33
+ output = generator(prompt, max_new_tokens=100, do_sample=True)[0]['generated_text']
34
+ return output.strip().split("\n")[0]
35
 
36
  # Streamlit UI
37
+ st.set_page_config(page_title="Python Test Generator", page_icon="🐍")
38
+ st.title("🐍 Python Test Generator (Gemma)")
39
+ st.markdown("Powered by Google's Gemma LLM")
40
 
41
  name = st.text_input("Enter your name")
42
  difficulty = st.selectbox("Choose difficulty level", ["easy", "medium", "hard"])
 
45
  if not name:
46
  st.warning("Please enter your name.")
47
  else:
48
+ question = generate_question(difficulty)
49
  st.session_state["question"] = question
50
 
 
51
  if "question" in st.session_state:
52
+ st.subheader("Generated Question")
53
  st.write(st.session_state["question"])
 
54
  answer = st.text_area("Your Answer")
55
+
56
  if st.button("Submit Answer"):
57
+ entry = {
58
  "name": name,
59
  "difficulty": difficulty,
60
  "question": st.session_state["question"],
61
  "answer": answer
62
  }
63
  data = load_data()
64
+ data.append(entry)
65
  save_data(data)
66
+ st.success("Your answer has been saved!")