aicodingfun commited on
Commit
94d357f
·
verified ·
1 Parent(s): 0ed8112

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -19
app.py CHANGED
@@ -4,16 +4,31 @@ import json
4
  import os
5
  import random
6
  import gradio as gr
 
 
7
 
8
  # 取得 Hugging Face Token,用於 API 調用推論服務
9
- from huggingface_hub import InferenceClient
10
 
11
- API_KEY = os.environ.get("GROQ_API_KEY")
12
- API_BASE_URL = os.environ.get("GROQ_API_BASE_URL")
13
- client = InferenceClient(
14
- api_key=API_KEY,
15
- base_url=API_BASE_URL
16
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
17
 
18
  # 載入問答資料庫路徑
19
  output_dir = "./question_bank"
@@ -98,7 +113,7 @@ def generate_math_questions(grade, term, qtype="Unspecified", num_questions=10):
98
  print(f"Retrieved {len(raw_questions)} document(s)")
99
 
100
  # 隨機選取並限制字串長度的題庫內容
101
- input_question_bank = random_questions_with_limit(raw_questions, 1000)
102
 
103
  # 系統訊息 (system) 與使用者要求 (user) 的對話內容
104
  # 說明給模型:要產生新的英文題目,保持難度與風格類似,但不要直接抄題庫原文
@@ -106,7 +121,7 @@ def generate_math_questions(grade, term, qtype="Unspecified", num_questions=10):
106
  {
107
  "role": "system",
108
  "content": """
109
- You are an advanced AI assistant designed to generate educational questions. Your task is to create new and diverse questions based on existing question banks. The new questions should be similar in difficulty, style, and structure to the input questions but must not directly replicate them. **All responses must be in English.**
110
 
111
  Key Instructions:
112
  1. Maintain the educational purpose of the questions.
@@ -124,7 +139,7 @@ def generate_math_questions(grade, term, qtype="Unspecified", num_questions=10):
124
  - Clearly distinguish the new question and the answer.
125
  - If multiple questions are generated, number them sequentially.
126
  - Ensure clarity and correctness in all generated content.
127
- - Respond only in **English**."""
128
  },
129
  {
130
  "role": "user",
@@ -160,16 +175,23 @@ def generate_math_questions(grade, term, qtype="Unspecified", num_questions=10):
160
  }
161
  ]
162
 
163
- # 使用 InferenceClient 呼叫 API 模型產生新題目
164
- completion = client.chat.completions.create(
165
- # model="mistralai/Mistral-7B-Instruct-v0.3",
166
- model="deepseek-r1-distill-qwen-32b",
167
- # model="mistralai/Mistral-Nemo-Instruct-2407",
168
- messages=messages,
169
- max_tokens=1024
170
- )
171
 
172
- response = completion.choices[0].message.content
 
 
 
 
 
 
 
173
  response = remove_think_tags(response)
174
  print(response)
175
 
 
4
  import os
5
  import random
6
  import gradio as gr
7
+ from google import genai
8
+ from google.genai import types
9
 
10
  # 取得 Hugging Face Token,用於 API 調用推論服務
11
+ # from huggingface_hub import InferenceClient
12
 
13
+ # API_KEY = os.environ.get("GROQ_API_KEY")
14
+ # API_BASE_URL = os.environ.get("GROQ_API_BASE_URL")
15
+
16
+ # client = InferenceClient(
17
+ # api_key=API_KEY,
18
+ # base_url=API_BASE_URL
19
+ # )
20
+
21
+ API_KEY = os.environ.get("GOOGLE_API_KEY")
22
+ client = genai.Client(api_key=API_KEY)
23
+
24
+ def generate_content(model="gemini-2.0-pro-exp-02-05", sys_prompt=None, user_prompt=None):
25
+ response = client.models.generate_content(
26
+ model=model,
27
+ config=types.GenerateContentConfig(
28
+ system_instruction=sys_prompt),
29
+ contents=user_prompt
30
+ )
31
+ return response.text
32
 
33
  # 載入問答資料庫路徑
34
  output_dir = "./question_bank"
 
113
  print(f"Retrieved {len(raw_questions)} document(s)")
114
 
115
  # 隨機選取並限制字串長度的題庫內容
116
+ input_question_bank = random_questions_with_limit(raw_questions, 5000)
117
 
118
  # 系統訊息 (system) 與使用者要求 (user) 的對話內容
119
  # 說明給模型:要產生新的英文題目,保持難度與風格類似,但不要直接抄題庫原文
 
121
  {
122
  "role": "system",
123
  "content": """
124
+ You are an advanced AI assistant designed to generate educational questions. Your task is to create new and diverse questions based on existing question banks. The new questions should be similar in difficulty, style, and structure to the input questions but must not directly replicate them. **All responses must be in Traditional Chinese.**
125
 
126
  Key Instructions:
127
  1. Maintain the educational purpose of the questions.
 
139
  - Clearly distinguish the new question and the answer.
140
  - If multiple questions are generated, number them sequentially.
141
  - Ensure clarity and correctness in all generated content.
142
+ - Respond only in **Traditional Chinese**."""
143
  },
144
  {
145
  "role": "user",
 
175
  }
176
  ]
177
 
178
+ # # 使用 InferenceClient 呼叫 API 模型產生新題目
179
+ # completion = client.chat.completions.create(
180
+ # # model="mistralai/Mistral-7B-Instruct-v0.3",
181
+ # model="deepseek-r1-distill-qwen-32b",
182
+ # # model="mistralai/Mistral-Nemo-Instruct-2407",
183
+ # messages=messages,
184
+ # max_tokens=1024
185
+ # )
186
 
187
+ # response = completion.choices[0].message.content
188
+
189
+ response = generate_content(
190
+ sys_prompt=messages[0]['content'],
191
+ user_prompt=messages[1]['content']
192
+ )
193
+ print(response)
194
+
195
  response = remove_think_tags(response)
196
  print(response)
197