Update api.py
Browse files
api.py
CHANGED
@@ -211,6 +211,92 @@ The description of the problem should not exceed 100 words or so."""
|
|
211 |
|
212 |
return prompt
|
213 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
214 |
# --- API Endpoint ---
|
215 |
# API state check route
|
216 |
@app.get("/")
|
|
|
211 |
|
212 |
return prompt
|
213 |
|
214 |
+
def evaluation(descriptions: list[str]):
|
215 |
+
# Check if Gemini library was imported and configured
|
216 |
+
if not genai:
|
217 |
+
logger.error("Gemini client is not available or configured.")
|
218 |
+
raise HTTPException(status_code=503, detail="Service Unavailable: Gemini client not configured")
|
219 |
+
|
220 |
+
logger.info("Received request for specificity evaluation.")
|
221 |
+
interrogative_probs = []
|
222 |
+
|
223 |
+
# --- 1. Generate Problematics using Gemini ---
|
224 |
+
for description in descriptions:
|
225 |
+
prompt = f"""I want you to create a technical problematic using a problem description.
|
226 |
+
Here is the problem description to deal with: <problem_description>{description}</problem_description>
|
227 |
+
|
228 |
+
The problematic must be in interrogative form.
|
229 |
+
As the output, I only want you to provide the problematic found, nothing else.
|
230 |
+
|
231 |
+
Here are examples of problematics that you could create, it shows the level of specificity that you should aim for:
|
232 |
+
|
233 |
+
Example 1: 'How can a method for allocating radio resources in a non-GSO satellite communication system, operating in frequency bands shared with geostationary satellite systems and particularly in high frequency bands such as Ka, minimize interference to geostationary systems, without causing reduced geographic coverage due to fixed high separation angle thresholds or incurring cost and sub-optimality from over-dimensioning the non-GSO satellite constellation?'
|
234 |
+
Example 2: 'How to address the vulnerability of on-aircraft avionics software update integrity checks to system compromises and the logistical challenges of cryptographic key management in digital signature solutions, in order to establish a more secure and logistically efficient method for updating safety-critical avionics equipment?'
|
235 |
+
Example 3: 'How can SIM cards be protected against collision attacks that aim to retrieve the secret key Ki by analyzing the input and output of the authentication algorithm during the standard GSM authentication process, given that current tamper-proof measures are insufficient to prevent this type of key extraction?'
|
236 |
+
Example 4: 'How can a Trusted Application in a GlobalPlatform compliant TEE overcome the GP specification limitations that enforce client blocking during task execution, prevent partial task execution, and delete TA execution context between commands, to function as a persistent server with stateful sessions and asynchronous communication capabilities, thereby enabling server functionalities like continuous listening and non-blocking send/receive, currently impossible due to GP's sequential task processing and stateless TA operation?'
|
237 |
+
|
238 |
+
As far as possible, avoid using acronyms in the problematic.
|
239 |
+
Try to be about the same length as the examples if possible."""
|
240 |
+
|
241 |
+
try:
|
242 |
+
logger.info("Calling Gemini API to generate problematic...")
|
243 |
+
# Use the specified model and configuration
|
244 |
+
model_name = "gemini-2.5-flash-preview-04-17"
|
245 |
+
model = genai.GenerativeModel(model_name)
|
246 |
+
|
247 |
+
response = model.generate_content(prompt)
|
248 |
+
# Extract the result
|
249 |
+
problematic_result = response.text.strip()
|
250 |
+
interrogative_probs.append(problematic_result)
|
251 |
+
logger.info("Successfully generated problematic from Gemini.")
|
252 |
+
logger.debug(f"Generated problematic: {problematic_result[:200]}...") # Log snippet
|
253 |
+
|
254 |
+
except Exception as e:
|
255 |
+
logger.error(f"Error calling Gemini API: {e}", exc_info=True)
|
256 |
+
# Check for specific Gemini API errors if possible
|
257 |
+
raise HTTPException(status_code=502, detail=f"Failed to generate problematic using LLM: {e}")
|
258 |
+
|
259 |
+
if not problematic_result:
|
260 |
+
logger.error("Gemini API returned an empty result.")
|
261 |
+
raise HTTPException(status_code=502, detail="LLM returned an empty problematic.")
|
262 |
+
|
263 |
+
# --- 2. Evaluate Specificity using External API ---
|
264 |
+
API_URL = "https://organizedprogrammers-fastapi-problematic-specificity.hf.space"
|
265 |
+
endpoint = f"{API_URL}/predict"
|
266 |
+
data = {"text": problematic_result}
|
267 |
+
|
268 |
+
try:
|
269 |
+
logger.info(f"Calling specificity prediction API at {endpoint}...")
|
270 |
+
prediction_response = requests.post(endpoint, json=data, timeout=30) # Added timeout
|
271 |
+
prediction_response.raise_for_status() # Raise HTTPError for bad responses (4xx or 5xx)
|
272 |
+
|
273 |
+
score_data = prediction_response.json()
|
274 |
+
logger.info(f"Successfully received specificity score: {score_data}")
|
275 |
+
|
276 |
+
# Validate the received score data against Pydantic model
|
277 |
+
try:
|
278 |
+
specificity_score = SpecificityScore(**score_data)
|
279 |
+
except Exception as pydantic_error: # Catch validation errors
|
280 |
+
logger.error(f"Failed to validate specificity score response: {pydantic_error}", exc_info=True)
|
281 |
+
logger.error(f"Invalid data received from specificity API: {score_data}")
|
282 |
+
raise HTTPException(status_code=502, detail="Invalid response format from specificity prediction API.")
|
283 |
+
|
284 |
+
except requests.exceptions.RequestException as e:
|
285 |
+
logger.error(f"Error calling specificity prediction API: {e}", exc_info=True)
|
286 |
+
raise HTTPException(status_code=502, detail=f"Failed to call specificity prediction API: {e}")
|
287 |
+
except Exception as e: # Catch other potential errors like JSON decoding
|
288 |
+
logger.error(f"Unexpected error during specificity evaluation: {e}", exc_info=True)
|
289 |
+
raise HTTPException(status_code=500, detail=f"Internal error during specificity evaluation: {e}")
|
290 |
+
|
291 |
+
|
292 |
+
# --- 3. Return Combined Result ---
|
293 |
+
final_response = SpecificityEvaluationResponse(
|
294 |
+
problematic=problematic_result,
|
295 |
+
specificity=specificity_score
|
296 |
+
)
|
297 |
+
return final_response
|
298 |
+
|
299 |
+
|
300 |
# --- API Endpoint ---
|
301 |
# API state check route
|
302 |
@app.get("/")
|