Spaces:
Sleeping
Sleeping
| import os | |
| from huggingface_hub import InferenceClient | |
| def generate_dropout_insights(input_values, risk_factors, label): | |
| """ | |
| Generates structured dropout risk insights using AI, ensuring Unicode-safe text for PDFs. | |
| """ | |
| excluded_fields = {"roll_no", "semester", "degree"} # Fields to exclude from analysis | |
| # Filter only relevant input values | |
| filtered_input_values = {key: value for key, value in input_values.items() if key not in excluded_fields} | |
| # Map input values to their corresponding risk factors | |
| risk_mapping = {key: risk for key, risk in zip(filtered_input_values.keys(), risk_factors)} | |
| # Construct message for AI model | |
| formatted_input = "\n".join([f"{key}: {value} (Risk: {risk})" for key, (value, risk) in zip(filtered_input_values.keys(), zip(filtered_input_values.values(), risk_factors))]) | |
| messages = [ | |
| { | |
| "role": "user", | |
| "content": f""" | |
| Generate a detailed dropout risk analysis report based on the following data: | |
| {formatted_input} | |
| Format the response as: | |
| ## Analysis of Student Dropout Risk | |
| Overall Risk Level: {label} | |
| ### π’ Strengths: | |
| List areas where the student is performing well: | |
| {{strengths}} | |
| ### π΄ Concerns: | |
| Identify factors that increase dropout risk: | |
| {{concerns}} | |
| ### π Action Plan for Improvement: | |
| Provide targeted **solutions** based on the student's **weak areas**: | |
| {{recommendations}} | |
| ## π Final Recommendation: | |
| {{overall_recommendation}} | |
| """ | |
| } | |
| ] | |
| client = InferenceClient( | |
| provider="nebius", | |
| api_key=os.getenv("HUGGINGFACE_API_TOKEN") | |
| ) | |
| stream = client.chat.completions.create( | |
| model="deepseek-ai/DeepSeek-R1", | |
| messages=messages, | |
| temperature=0.5, | |
| max_tokens=2048, | |
| top_p=0.7, | |
| stream=True | |
| ) | |
| # Collect and return the generated report | |
| report = "" | |
| for chunk in stream: | |
| report += chunk.choices[0].delta.content | |
| return report | |