Spaces:
Sleeping
Sleeping
File size: 2,756 Bytes
8508fef 541d869 48d1c1b 0da569a 047d4a1 aae4b3f 0da569a a394d98 aae4b3f 0da569a 541d869 0da569a 541d869 c675bc7 0da569a c675bc7 0da569a 541d869 a394d98 0da569a a394d98 0da569a c675bc7 541d869 0da569a 541d869 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
import streamlit as st
from openai import OpenAI
import time
# Replace with your Grok API key, considering Hugging Face's secrets management
grok_api_key = st.secrets["GROK_API_KEY"]
client = OpenAI(
api_key=grok_api_key,
base_url="https://api.x.ai/v1",
)
def generate_question(topic):
prompt = f"Generate a multiple-choice question about {topic}"
response = client.completions.create(
model="grok-beta",
prompt=prompt,
max_tokens=150, # Limit the response length
n=1, # Generate only 1 response
stop=None, # No specific stop sequence
)
question = response.choices[0].text.split("\n")[0]
choices = response.choices[0].text.split("\n")[1:]
return question, choices
def game_mode(topic):
st.write("Game Mode: Answer quickly!")
# Start a timer for the game mode (30 seconds per question)
start_time = time.time()
question, choices = generate_question(topic)
st.write(question)
# Use session state to track whether the answer has been submitted
if 'submitted' not in st.session_state:
st.session_state.submitted = False
if not st.session_state.submitted:
answer = st.radio("Select your answer:", choices)
submit_button = st.button("Submit")
if submit_button:
st.session_state.submitted = True
st.write("You selected:", answer)
time_elapsed = time.time() - start_time
st.write(f"Time taken: {int(time_elapsed)} seconds")
if time_elapsed >= 30:
st.warning("Time's up!")
else:
st.success("Good job!")
else:
st.write("You've already submitted your answer.")
def quiz_mode(topic):
st.write("Quiz Mode: Take your time!")
question, choices = generate_question(topic)
st.write(question)
# Use session state to track whether the answer has been submitted
if 'submitted' not in st.session_state:
st.session_state.submitted = False
if not st.session_state.submitted:
answer = st.radio("Select your answer:", choices)
submit_button = st.button("Submit")
if submit_button:
st.session_state.submitted = True
st.write("You selected:", answer)
st.success("Great! On to the next question!")
else:
st.write("You've already submitted your answer.")
def main():
st.title("Grok-Powered Quiz App")
mode = st.selectbox("Choose the mode", ["Game Mode", "Quiz Mode"])
topic = st.text_input("Enter a topic:")
if topic:
if mode == "Game Mode":
game_mode(topic)
else:
quiz_mode(topic)
if __name__ == "__main__":
main()
|