WAQASCHANNA commited on
Commit
0da569a
·
verified ·
1 Parent(s): 10a4e97

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -71
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=GROK_API_KEY,
11
- base_url="https://api.x.ai/v1",
12
  )
13
 
14
- def get_grok_response(user_input):
15
- try:
16
- # Make the API call to Grok
17
- completion = client.chat.completions.create(
18
- model="grok-beta", # Specify the Grok model
19
- messages=[
20
- {"role": "system", "content": "You are Grok, a chatbot inspired by the Hitchhiker's Guide to the Galaxy."},
21
- {"role": "user", "content": user_input},
22
- ],
23
- )
24
-
25
- # Debug print the full response structure
26
- print(completion) # This will help you understand the structure
27
-
28
- # Assuming message is an object, let's access it correctly
29
- return completion.choices[0]['message']['content'] # Adjusting based on response format
30
-
31
- except Exception as e:
32
- return f"Error: {e}"
33
-
34
-
35
-
36
-
37
- # Streamlit UI for Quiz
38
- st.title("Grok Quiz Engine")
39
-
40
- # Welcome message
41
- st.write("Welcome to the Grok-powered Quiz Engine. Ask your questions, and Grok will assist you with the answers.")
42
-
43
- # Sidebar for Game-Like Settings
44
- st.sidebar.title("Settings")
45
- quiz_mode = st.sidebar.radio("Choose your mode:", ("Quiz Mode", "Game Mode"))
46
-
47
- if quiz_mode == "Game Mode":
48
- st.sidebar.write("In Game Mode, you'll get scored for each correct answer.")
49
- score = 0
50
- else:
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()