Spaces:
Paused
Paused
Update app.py
Browse files284 unmodified lines28528628728828929029129229329429512 unmodified lines30830931031131231331431512 unmodified lines32832933033133233333433568 unmodified lines4044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634643 unmodified lines468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522284 unmodified lines# if __name__ == "__main__":# iface.launch()import gradio as grimport osfrom mistralai.client import MistralClientfrom mistralai.models.chat_completion import ChatMessage# Mistral AI setupapi_key = os.getenv("MISTRAL_API_KEY")if not api_key:12 unmodified linesexcept Exception as e:return f"An error occurred: {str(e)}"# HTML content with interactive visualizationhtml_content = """<!DOCTYPE html><html lang="en">12 unmodified lines<div id="visualization"></div><div id="generatedGoals"></div><script>// Define the goals and connections dataconst goals = [{ id: 1, x: 100, y: 400, name: "Automate Data Import", description: "Develop scripts to automate exam data extraction from various sources (CSV, Excel, databases) using Pandas read_* functions." },{ id: 2, x: 200, y: 300, name: "Data Cleaning", description: "Implement robust data cleaning processes to handle missing values, outliers, and inconsistencies in exam data using Pandas methods like dropna(), fillna(), and apply()." },{ id: 3, x: 300, y: 200, name: "Data Transformation", description: "Utilize Pandas for complex data transformations such as pivoting exam results, melting question-wise scores, and creating derived features for analysis." },68 unmodified lines{ source: 24, target: 17 },{ source: 26, target: 29 }];// Create the SVG container for the goals and connectionsconst svg = d3.select("#goalSpace").append("svg").attr("width", width).attr("height", height);// Draw connections between goalsconst 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 nodesconst 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 goalsconst 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 boxconst hoverInfo = d3.select("#hoverInfo");// Add hover effects on goal nodesgoalNodes.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");});node.on("click", async function(event, d) {const response = await fetch('/generate_goals', {method: 'POST',3 unmodified linesconst data = await response.json();document.getElementById("generatedGoals").innerHTML = `<h2>Generated Goals for ${d.name}</h2><pre>${data.goals}</pre>`;});// Function to update selected goal informationfunction updateSelectedGoalInfo(goal) {const selectedGoalDiv = d3.select("#selectedGoal");selectedGoalDiv.html(`<h3>${goal.name}</h3><p>${goal.description}</p>`);}// Function to display the response from the serverfunction displayResponse(content) {const responseBox = d3.select("#responseBox");responseBox.html(`<h2>Response</h2><p>${content}</p>`);}// Handle mouse move event to highlight the closest goalsvg.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 pointerfunction 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 goalfunction highlightClosestGoal(goal) {d3.select("#info").html(`Closest goal: ${goal.name}`);}</script></body></html>"""# Gradio interfaceiface = gr.Interface(fn=generate_goals,inputs="text",outputs="text",title="Exam Data Analysis Goals Generator",description="Click on a goal in the visualization to generate related goals.",article=html_content)# Launch the Gradio appiface.launch()284 unmodified lines28528628728828929029129229329429529629712 unmodified lines31031131231331431512 unmodified lines32832933033133233333433533668 unmodified lines4054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414423 unmodified lines446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492284 unmodified lines# if __name__ == "__main__":# iface.launch()from flask import Flask, request, jsonify, render_template_stringimport osfrom mistralai.client import MistralClientfrom mistralai.models.chat_completion import ChatMessageapp = Flask(__name__)# Mistral AI setupapi_key = os.getenv("MISTRAL_API_KEY")if not api_key:12 unmodified linesexcept Exception as e:return f"An error occurred: {str(e)}"html_content = """<!DOCTYPE html><html lang="en">12 unmodified lines<div id="visualization"></div><div id="generatedGoals"></div><script>const width = 1200;const height = 800;const goals = [{ id: 1, x: 100, y: 400, name: "Automate Data Import", description: "Develop scripts to automate exam data extraction from various sources (CSV, Excel, databases) using Pandas read_* functions." },{ id: 2, x: 200, y: 300, name: "Data Cleaning", description: "Implement robust data cleaning processes to handle missing values, outliers, and inconsistencies in exam data using Pandas methods like dropna(), fillna(), and apply()." },{ id: 3, x: 300, y: 200, name: "Data Transformation", description: "Utilize Pandas for complex data transformations such as pivoting exam results, melting question-wise scores, and creating derived features for analysis." },68 unmodified lines{ source: 24, target: 17 },{ source: 26, target: 29 }];const svg = d3.select("#visualization").append("svg").attr("width", width).attr("height", height);const simulation = d3.forceSimulation(goals).force("link", d3.forceLink(connections).id(d => d.id)).force("charge", d3.forceManyBody().strength(-400)).force("center", d3.forceCenter(width / 2, height / 2));const link = svg.append("g").selectAll("line").data(connections).enter().append("line").attr("stroke", "#999").attr("stroke-opacity", 0.6);const node = svg.append("g").selectAll("circle").data(goals).enter().append("circle").attr("r", 10).attr("fill", d => d.color).call(d3.drag().on("start", dragstarted).on("drag", dragged).on("end", dragended));const text = svg.append("g").selectAll("text").data(goals).enter().append("text").text(d => d.name).attr("font-size", "12px").attr("dx", 12).attr("dy", 4);node.on("click", async function(event, d) {const response = await fetch('/generate_goals', {method: 'POST',3 unmodified linesconst data = await response.json();document.getElementById("generatedGoals").innerHTML = `<h2>Generated Goals for ${d.name}</h2><pre>${data.goals}</pre>`;});simulation.on("tick", () => {link.attr("x1", d => d.source.x).attr("y1", d => d.source.y).attr("x2", d => d.target.x).attr("y2", d => d.target.y);node.attr("cx", d => d.x).attr("cy", d => d.y);text.attr("x", d => d.x).attr("y", d => d.y);});function dragstarted(event) {if (!event.active) simulation.alphaTarget(0.3).restart();event.subject.fx = event.subject.x;event.subject.fy = event.subject.y;}function dragged(event) {event.subject.fx = event.x;event.subject.fy = event.y;}function dragended(event) {if (!event.active) simulation.alphaTarget(0);event.subject.fx = null;event.subject.fy = null;}</script></body></html>"""@app.route('/')def index():return render_template_string(html_content)@app.route('/generate_goals', methods=['POST'])def generate_goals_api():input_var = request.json['input_var']goals = generate_goals(input_var)return jsonify({'goals': goals})if __name__ == "__main__":app.run(host='0.0.0.0', port=7860)
app.py+76-106