Spaces:
Building
Building
File size: 7,837 Bytes
a8335ab |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
import gradio as gr
import os
from mistralai.client import MistralClient
from mistralai.models.chat_completion import ChatMessage
# Ensure the environment variable for the API key is set
api_key = os.getenv("MISTRAL_API_KEY")
if not api_key:
raise ValueError("MISTRAL_API_KEY environment variable not set")
model = "mistral-tiny"
client = MistralClient(api_key=api_key)
def generate_goals(input_var):
messages = [
ChatMessage(role="user", content=f"Generate 10 specific, industry relevant goals for {input_var} using Python and Pandas. Each goal should include a brief name and a one-sentence description of the task or skill. Focus on practical applications in educational assessment, covering areas such as data processing, statistical analysis, visualization, and advanced techniques")
]
try:
response = client.chat(model=model, messages=messages)
content = response.choices[0].message.content
return content
except Exception as e:
return f"An error occurred: {str(e)}"
# HTML content
html_content = """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Comprehensive Exam Data Analysis with Pandas - 30 Industry Goals with Connections</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/7.8.5/d3.min.js"></script>
<style>
body { font-family: Arial, sans-serif; margin: 20px; }
#goalSpace { border: 1px solid #ccc; margin-bottom: 20px; }
.goal { cursor: pointer; }
#info { margin-top: 20px; font-weight: bold; }
#selectedGoal { margin-top: 10px; padding: 10px; border: 1px solid #ccc; background-color: #f0f0f0; }
#hoverInfo {
position: absolute;
padding: 10px;
background-color: rgba(255, 255, 255, 0.9);
border: 1px solid #ccc;
border-radius: 5px;
font-size: 14px;
max-width: 300px;
display: none;
}
#responseBox {
margin-top: 20px;
padding: 10px;
border: 1px solid #ccc;
background-color: #e0f7fa;
}
</style>
</head>
<body>
<h1>Comprehensive Exam Data Analysis with Pandas - 30 Industry Goals with Connections</h1>
<div id="goalSpace"></div>
<div id="info"></div>
<div id="selectedGoal"></div>
<div id="hoverInfo"></div>
<div id="responseBox"></div>
<script>
const width = 1200;
const height = 800;
// Define the goals and connections data
const goals = [
// ... (your goals data here)
];
const connections = [
// ... (your connections data here)
];
// Create the SVG container for the goals and connections
const svg = d3.select("#goalSpace")
.append("svg")
.attr("width", width)
.attr("height", height);
// Draw connections between goals
const links = svg.selectAll("line")
.data(connections)
.enter()
.append("line")
.attr("x1", d => goals.find(g => g.id === d.source).x)
.attr("y1", d => goals.find(g => g.id === d.source).y)
.attr("x2", d => goals.find(g => g.id === d.target).x)
.attr("y2", d => goals.find(g => g.id === d.target).y)
.attr("stroke", "#999")
.attr("stroke-width", 1)
.attr("stroke-opacity", 0.6);
// Draw goal nodes
const goalNodes = svg.selectAll("circle")
.data(goals)
.enter()
.append("circle")
.attr("cx", d => d.x)
.attr("cy", d => d.y)
.attr("r", 10)
.attr("fill", d => {
if (d.id <= 10) return "blue";
if (d.id <= 20) return "green";
return "orange";
})
.attr("class", "goal");
// Add labels to the goals
const goalLabels = svg.selectAll("text")
.data(goals)
.enter()
.append("text")
.attr("x", d => d.x + 15)
.attr("y", d => d.y)
.text(d => d.name)
.attr("font-size", "12px");
// Hover info box
const hoverInfo = d3.select("#hoverInfo");
// Add hover effects on goal nodes
goalNodes.on("mouseover", function(event, d) {
d3.select(this).attr("r", 15);
hoverInfo.style("display", "block")
.style("left", (event.pageX + 10) + "px")
.style("top", (event.pageY - 10) + "px")
.html(`<strong>${d.name}</strong><br>${d.description}`);
}).on("mouseout", function() {
d3.select(this).attr("r", 10);
hoverInfo.style("display", "none");
});
// Handle click event on goal nodes
goalNodes.on("click", async function(event, d) {
updateSelectedGoalInfo(d);
try {
const response = await fetch('generate_goals', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ input_var: d.name })
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
displayResponse(data.content);
} catch (error) {
console.error("There was an error fetching the response:", error);
displayResponse("An error occurred while generating the response.");
}
});
// Function to update selected goal information
function updateSelectedGoalInfo(goal) {
const selectedGoalDiv = d3.select("#selectedGoal");
selectedGoalDiv.html(`
<h3>${goal.name}</h3>
<p>${goal.description}</p>
`);
}
// Function to display the response from the server
function displayResponse(content) {
const responseBox = d3.select("#responseBox");
responseBox.html(`
<h2>Response</h2>
<p>${content}</p>
`);
}
// Handle mouse move event to highlight the closest goal
svg.on("mousemove", function(event) {
const [x, y] = d3.pointer(event);
const closest = findClosestGoal(x, y);
highlightClosestGoal(closest);
});
// Function to find the closest goal to the mouse pointer
function findClosestGoal(x, y) {
return goals.reduce((closest, goal) => {
const distance = Math.sqrt(Math.pow(goal.x - x, 2) + Math.pow(goal.y - y, 2));
return distance < closest.distance ? { goal, distance } : closest;
}, { goal: null, distance: Infinity }).goal;
}
// Function to highlight the closest goal
function highlightClosestGoal(goal) {
d3.select("#info").html(`Closest goal: ${goal.name}`);
}
</script>
</body>
</html>
"""
# Gradio interface
iface = gr.Interface(
fn=generate_goals,
inputs=gr.Textbox(label="Goal Name"),
outputs=gr.Textbox(label="Generated Goals"),
title="Exam Data Analysis Goals Generator",
description="Click on a goal in the visualization to generate related goals.",
allow_flagging="never",
theme="default",
css=html_content
)
if __name__ == "__main__":
iface.launch() |