harris1 commited on
Commit
a8335ab
1 Parent(s): 17f5828

Create app.py

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