Rathapoom commited on
Commit
30cf447
·
verified ·
1 Parent(s): 92d6853

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -14
app.py CHANGED
@@ -1,27 +1,31 @@
1
  import streamlit as st
2
  import openai
3
 
4
- # Set your OpenAI API key
5
  openai.api_key = st.secrets["OPENAI_API_KEY"]
6
 
 
 
 
7
  # Streamlit app
8
- st.title("Test OpenAI API in Streamlit")
9
 
10
- # User input
11
- user_input = st.text_area("Enter prompt to generate questions:")
12
 
13
- if st.button("Generate Response"):
14
  if user_input:
15
- # Create a chat completion
16
- completion = openai.ChatCompletion.create(
17
- model="gpt-4o-mini", # Or use "gpt-4" if available to you
18
  messages=[
19
- {"role": "system", "content": "You are a helpful assistant."},
20
- {"role": "user", "content": user_input}
21
  ]
22
  )
23
- # Display the assistant's reply
24
- st.write("Assistant's response:")
25
- st.write(completion.choices[0].message['content'])
 
26
  else:
27
- st.warning("Please enter a prompt.")
 
1
  import streamlit as st
2
  import openai
3
 
4
+ # Set your OpenAI API key using Streamlit secrets
5
  openai.api_key = st.secrets["OPENAI_API_KEY"]
6
 
7
+ # Create an OpenAI client using the new API structure
8
+ client = openai.OpenAI(api_key=openai.api_key)
9
+
10
  # Streamlit app
11
+ st.title("Exam Question Generator")
12
 
13
+ # Input field for user prompt
14
+ user_input = st.text_area("Enter knowledge material to generate exam questions:")
15
 
16
+ if st.button("Generate Questions"):
17
  if user_input:
18
+ # Call OpenAI's Chat Completion API using the new structure
19
+ response = client.chat.completions.create(
20
+ model="gpt-4o-mini", # Or use "gpt-4" if available
21
  messages=[
22
+ {"role": "system", "content": "You are a helpful assistant for generating exam questions."},
23
+ {"role": "user", "content": f"Generate exam questions from the following material: {user_input}"}
24
  ]
25
  )
26
+
27
+ # Extract the response and display it
28
+ st.write("Generated Exam Questions:")
29
+ st.write(response.choices[0].message['content'])
30
  else:
31
+ st.warning("Please enter the knowledge material.")