Yahiya commited on
Commit
d16f623
Β·
verified Β·
1 Parent(s): ed96138

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -67
app.py CHANGED
@@ -1,68 +1,49 @@
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
- # Automatically choose float16 if GPU is available
12
- dtype = torch.float16 if torch.cuda.is_available() else torch.float32
13
- tokenizer = AutoTokenizer.from_pretrained(model_id)
14
- model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.float32) # use float16 if on GPU
15
- pipe = TextGenerationPipeline(model=model, tokenizer=tokenizer, device=0 if torch.cuda.is_available() else -1)
16
- return pipe
17
-
18
- generator = load_gemma()
19
-
20
- # Data storage
21
- DATA_FILE = "user_data.json"
22
-
23
- def load_data():
24
- if os.path.exists(DATA_FILE):
25
- with open(DATA_FILE, "r") as f:
26
- return json.load(f)
27
- return []
28
-
29
- def save_data(data):
30
- with open(DATA_FILE, "w") as f:
31
- json.dump(data, f, indent=2)
32
-
33
- def generate_question(difficulty):
34
- prompt = f"Generate a {difficulty} level Python coding test question:\n"
35
- output = generator(prompt, max_new_tokens=100, do_sample=True)[0]['generated_text']
36
- return output.strip().split("\n")[0]
37
-
38
- # Streamlit UI
39
- st.set_page_config(page_title="Python Test Generator", page_icon="🐍")
40
- st.title("🐍 Python Test Generator (Gemma)")
41
- st.markdown("Powered by Google's Gemma LLM")
42
-
43
- name = st.text_input("Enter your name")
44
- difficulty = st.selectbox("Choose difficulty level", ["easy", "medium", "hard"])
45
-
46
- if st.button("Generate Question"):
47
- if not name:
48
- st.warning("Please enter your name.")
49
- else:
50
- question = generate_question(difficulty)
51
- st.session_state["question"] = question
52
-
53
- if "question" in st.session_state:
54
- st.subheader("Generated Question")
55
- st.write(st.session_state["question"])
56
- answer = st.text_area("Your Answer")
57
-
58
- if st.button("Submit Answer"):
59
- entry = {
60
- "name": name,
61
- "difficulty": difficulty,
62
- "question": st.session_state["question"],
63
- "answer": answer
64
- }
65
- data = load_data()
66
- data.append(entry)
67
- save_data(data)
68
- st.success("Your answer has been saved!")
 
1
  import streamlit as st
2
+ import google.generativeai as genai
3
+
4
+ st.set_page_config(page_title="Gemini Prompt Generator", layout="centered")
5
+
6
+ st.title("🌟 Gemini Prompt Generator")
7
+
8
+ # User input section
9
+ api_key = st.text_input("πŸ”‘ Enter your Gemini API Key", type="password")
10
+
11
+ if api_key:
12
+ genai.configure(api_key=api_key)
13
+
14
+ prompt = st.text_area("πŸ’¬ Enter your prompt", "What is the velocity of an unladen swallow?")
15
+ system_instructions = st.text_area("🧠 System Instructions", "You have a tendency to speak in riddles.")
16
+
17
+ model = st.selectbox("πŸ€– Choose Gemini model", [
18
+ "gemini-1.0-pro",
19
+ "gemini-1.5-pro",
20
+ "gemini-1.5-flash",
21
+ "gemini-2.0-flash"
22
+ ])
23
+
24
+ temperature = st.slider("🌑️ Temperature", min_value=0.0, max_value=2.0, value=0.5, step=0.05)
25
+ stop_sequence = st.text_input("β›” Stop Sequence (optional)", "")
26
+
27
+ # Handle unsupported features
28
+ if model == "gemini-1.0-pro":
29
+ if system_instructions:
30
+ st.warning("System instructions ignored. gemini-1.0-pro does not support system instructions.")
31
+ system_instructions = None
32
+ if temperature > 1:
33
+ st.info("Temperature set to 1. gemini-1.0-pro does not support values > 1.")
34
+ temperature = 1
35
+
36
+ if stop_sequence.strip() == "":
37
+ stop_sequence = None
38
+
39
+ if st.button("πŸš€ Generate"):
40
+ try:
41
+ gen_model = genai.GenerativeModel(model, system_instruction=system_instructions)
42
+ config = genai.GenerationConfig(temperature=temperature, stop_sequences=[stop_sequence] if stop_sequence else [])
43
+ response = gen_model.generate_content(contents=[prompt], generation_config=config)
44
+ st.markdown("### ✨ Response")
45
+ st.write(response.text)
46
+ except Exception as e:
47
+ st.error(f"Error: {e}")
48
+ else:
49
+ st.info("Please enter your API key to continue.")