Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,14 +1,53 @@
|
|
1 |
import streamlit as st
|
2 |
import openai
|
3 |
import time
|
4 |
-
|
5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
openai.api_key = st.secrets["OPENAI_API_KEY"]
|
7 |
|
8 |
-
#
|
9 |
-
client = openai.OpenAI(api_key=openai.api_key)
|
10 |
-
|
11 |
-
# Function to generate exam questions using OpenAI API with retry logic
|
12 |
def generate_questions_with_retry(knowledge_material, question_type, cognitive_level, extra_instructions, case_based, num_choices=None, max_retries=3):
|
13 |
# Adjust the number of questions based on the type
|
14 |
if question_type == "Multiple Choice":
|
@@ -39,14 +78,16 @@ def generate_questions_with_retry(knowledge_material, question_type, cognitive_l
|
|
39 |
retries = 0
|
40 |
while retries < max_retries:
|
41 |
try:
|
42 |
-
response =
|
43 |
-
model="gpt-4o-mini",
|
44 |
messages=[
|
45 |
{"role": "system", "content": "You are a helpful assistant for generating exam questions."},
|
46 |
{"role": "user", "content": prompt}
|
47 |
-
]
|
|
|
|
|
48 |
)
|
49 |
-
return response.choices[0].message
|
50 |
except openai.error.APIConnectionError:
|
51 |
retries += 1
|
52 |
time.sleep(2) # Wait for 2 seconds before retrying
|
@@ -54,15 +95,18 @@ def generate_questions_with_retry(knowledge_material, question_type, cognitive_l
|
|
54 |
st.error("Failed to connect to OpenAI API after several attempts.")
|
55 |
return None
|
56 |
|
57 |
-
#
|
58 |
if 'username' not in st.session_state:
|
59 |
-
# Show the login form if the username is not set
|
60 |
st.title("Login")
|
61 |
username_input = st.text_input("Enter your username:")
|
62 |
if st.button("Login"):
|
63 |
if username_input:
|
64 |
-
|
65 |
-
|
|
|
|
|
|
|
|
|
66 |
else:
|
67 |
st.warning("Please enter a valid username.")
|
68 |
else:
|
@@ -83,8 +127,6 @@ else:
|
|
83 |
if uploaded_file.size > 5 * 1024 * 1024: # 5 MB limit
|
84 |
st.warning("File size exceeds 5 MB. Please upload a smaller file.")
|
85 |
else:
|
86 |
-
# Here you can add code to extract text from the PDF if needed
|
87 |
-
# For simplicity, we're focusing on the text input for now
|
88 |
st.success("File uploaded successfully! (Text extraction not implemented yet.)")
|
89 |
|
90 |
# Select question type
|
|
|
1 |
import streamlit as st
|
2 |
import openai
|
3 |
import time
|
4 |
+
import gspread
|
5 |
+
from oauth2client.service_account import ServiceAccountCredentials
|
6 |
+
|
7 |
+
# ตั้งค่า Google Sheets API
|
8 |
+
scope = ["https://spreadsheets.google.com/feeds", "https://www.googleapis.com/auth/drive"]
|
9 |
+
creds = ServiceAccountCredentials.from_json_keyfile_name("genexam-2c8c645ecc0d.json", scope) # เปลี่ยนเป็นชื่อไฟล์ JSON ของคุณ
|
10 |
+
client_gs = gspread.authorize(creds)
|
11 |
+
|
12 |
+
# เปิด Google Sheets โดยใช้ชื่อของ sheet
|
13 |
+
sheet = client_gs.open("GeneXam user").sheet1 # ใส่ชื่อจริงของ Google Sheet
|
14 |
+
|
15 |
+
# ฟังก์ชันตรวจสอบ username ใน Google Sheets
|
16 |
+
def check_user_in_sheet(username):
|
17 |
+
try:
|
18 |
+
users_list = sheet.col_values(1) # สมมติว่า usernames อยู่ในคอลัมน์แรก
|
19 |
+
if username in users_list:
|
20 |
+
return True
|
21 |
+
else:
|
22 |
+
return False
|
23 |
+
except Exception as e:
|
24 |
+
st.error(f"Error checking user: {str(e)}")
|
25 |
+
return False
|
26 |
+
|
27 |
+
# ฟังก์ชันอัปเดตการใช้ API ของผู้ใช้
|
28 |
+
def update_api_usage(username):
|
29 |
+
try:
|
30 |
+
users_list = sheet.col_values(1)
|
31 |
+
row_number = users_list.index(username) + 1 # หาตำแหน่งของ user
|
32 |
+
api_usage = int(sheet.cell(row_number, 2).value) # คอลัมน์ที่ 2 เป็นจำนวนการใช้ API
|
33 |
+
api_usage += 1
|
34 |
+
sheet.update_cell(row_number, 2, api_usage)
|
35 |
+
except Exception as e:
|
36 |
+
st.error(f"Error updating API usage: {str(e)}")
|
37 |
+
|
38 |
+
# รีเซ็ตการใช้ API เมื่อถึงเวลารีเซ็ตทุกวัน (ถ้าจำเป็น)
|
39 |
+
def reset_api_usage_daily():
|
40 |
+
try:
|
41 |
+
users_list = sheet.col_values(1)
|
42 |
+
for i, user in enumerate(users_list):
|
43 |
+
sheet.update_cell(i + 1, 2, 0) # รีเซ็ตคอลัมน์ที่ 2 (การใช้ API) เป็น 0
|
44 |
+
except Exception as e:
|
45 |
+
st.error(f"Error resetting API usage: {str(e)}")
|
46 |
+
|
47 |
+
# ตั้งค่า OpenAI API key จาก Hugging Face Secrets
|
48 |
openai.api_key = st.secrets["OPENAI_API_KEY"]
|
49 |
|
50 |
+
# ฟังก์ชันการสร้างข้อสอบโดยใช้ OpenAI API และมี retry logic
|
|
|
|
|
|
|
51 |
def generate_questions_with_retry(knowledge_material, question_type, cognitive_level, extra_instructions, case_based, num_choices=None, max_retries=3):
|
52 |
# Adjust the number of questions based on the type
|
53 |
if question_type == "Multiple Choice":
|
|
|
78 |
retries = 0
|
79 |
while retries < max_retries:
|
80 |
try:
|
81 |
+
response = openai.ChatCompletion.create(
|
82 |
+
model="gpt-4o-mini", # You can switch to gpt-4 if available in your plan
|
83 |
messages=[
|
84 |
{"role": "system", "content": "You are a helpful assistant for generating exam questions."},
|
85 |
{"role": "user", "content": prompt}
|
86 |
+
],
|
87 |
+
max_tokens=1000,
|
88 |
+
temperature=0.7
|
89 |
)
|
90 |
+
return response.choices[0].message["content"]
|
91 |
except openai.error.APIConnectionError:
|
92 |
retries += 1
|
93 |
time.sleep(2) # Wait for 2 seconds before retrying
|
|
|
95 |
st.error("Failed to connect to OpenAI API after several attempts.")
|
96 |
return None
|
97 |
|
98 |
+
# ระบบ login
|
99 |
if 'username' not in st.session_state:
|
|
|
100 |
st.title("Login")
|
101 |
username_input = st.text_input("Enter your username:")
|
102 |
if st.button("Login"):
|
103 |
if username_input:
|
104 |
+
if check_user_in_sheet(username_input):
|
105 |
+
st.session_state['username'] = username_input
|
106 |
+
st.success(f"Welcome, {username_input}!")
|
107 |
+
update_api_usage(username_input)
|
108 |
+
else:
|
109 |
+
st.warning("Username not found. Please try again.")
|
110 |
else:
|
111 |
st.warning("Please enter a valid username.")
|
112 |
else:
|
|
|
127 |
if uploaded_file.size > 5 * 1024 * 1024: # 5 MB limit
|
128 |
st.warning("File size exceeds 5 MB. Please upload a smaller file.")
|
129 |
else:
|
|
|
|
|
130 |
st.success("File uploaded successfully! (Text extraction not implemented yet.)")
|
131 |
|
132 |
# Select question type
|