Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,78 +1,48 @@
|
|
| 1 |
-
import os
|
| 2 |
import streamlit as st
|
| 3 |
from openai import OpenAI
|
| 4 |
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
# Set up the Grok API client
|
| 8 |
-
GROK_API_KEY = os.getenv("GROK_API_KEY")
|
| 9 |
client = OpenAI(
|
| 10 |
-
api_key=
|
| 11 |
-
base_url="https://api.x.ai/v1",
|
| 12 |
)
|
| 13 |
|
| 14 |
-
def
|
| 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 |
-
st.write(
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
st.
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
score = 0
|
| 52 |
-
|
| 53 |
-
# Quiz Question Section
|
| 54 |
-
st.subheader("Ask a Question")
|
| 55 |
-
|
| 56 |
-
# Text input for user to type a question
|
| 57 |
-
user_input = st.text_input("Enter your question:")
|
| 58 |
-
|
| 59 |
-
# Display AI response
|
| 60 |
-
if user_input:
|
| 61 |
-
response = get_grok_response(user_input)
|
| 62 |
-
st.write("**Grok's Response:**")
|
| 63 |
-
st.write(response)
|
| 64 |
-
|
| 65 |
-
if quiz_mode == "Game Mode":
|
| 66 |
-
# You can implement scoring logic based on correct answers (example: keyword match)
|
| 67 |
-
correct_answer_keywords = ["42", "meaning of life", "universe"]
|
| 68 |
-
if any(keyword.lower() in response.lower() for keyword in correct_answer_keywords):
|
| 69 |
-
score += 1
|
| 70 |
-
st.sidebar.success(f"Score: {score}")
|
| 71 |
-
else:
|
| 72 |
-
st.sidebar.warning(f"Score: {score}")
|
| 73 |
-
else:
|
| 74 |
-
st.write("Ask Grok a question to begin the quiz or game!")
|
| 75 |
-
|
| 76 |
-
# Footer with instructions or credits
|
| 77 |
-
st.write("---")
|
| 78 |
-
st.write("Created using Grok AI and Streamlit. Powered by Python!")
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from openai import OpenAI
|
| 3 |
|
| 4 |
+
# Replace with your Grok API key, considering Hugging Face's secrets management
|
| 5 |
+
grok_api_key = st.secrets["GROK_API_KEY"]
|
| 6 |
|
|
|
|
|
|
|
|
|
|
| 7 |
client = OpenAI(
|
| 8 |
+
api_key=grok_api_key,
|
| 9 |
+
base_url="https://api.x.ai/v1", # Grok base URL
|
| 10 |
)
|
| 11 |
|
| 12 |
+
def generate_question(topic):
|
| 13 |
+
prompt = f"Generate a multiple-choice question about {topic}"
|
| 14 |
+
response = client.chat.completions.create(
|
| 15 |
+
model="grok-beta", # Specify Grok model
|
| 16 |
+
messages=[
|
| 17 |
+
{"role": "system", "content": "You are Grok, a chatbot inspired by the Hitchhiker's Guide to the Galaxy."},
|
| 18 |
+
{"role": "user", "content": prompt},
|
| 19 |
+
],
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
# Debugging the structure of the response
|
| 23 |
+
st.write("Response:", response) # To see the raw structure
|
| 24 |
+
|
| 25 |
+
# Accessing the generated question and choices from the response
|
| 26 |
+
generated_text = response['choices'][0]['message']['content'] # Access the content properly
|
| 27 |
+
# Assume that the text contains the question and choices separated by newlines
|
| 28 |
+
question, *choices = generated_text.split("\n")
|
| 29 |
+
return question, choices
|
| 30 |
+
|
| 31 |
+
def main():
|
| 32 |
+
st.title("Grok-Powered Quiz App")
|
| 33 |
+
|
| 34 |
+
topic = st.text_input("Enter a topic:")
|
| 35 |
+
|
| 36 |
+
if topic:
|
| 37 |
+
question, choices = generate_question(topic)
|
| 38 |
+
|
| 39 |
+
st.write(question)
|
| 40 |
+
answer = st.radio("Select your answer:", choices)
|
| 41 |
+
|
| 42 |
+
if st.button("Submit"):
|
| 43 |
+
# Check the answer and provide feedback
|
| 44 |
+
st.write("You selected:", answer)
|
| 45 |
+
# Add feedback based on the correct answer (you'll need to implement this logic)
|
| 46 |
+
|
| 47 |
+
if __name__ == "__main__":
|
| 48 |
+
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|