Spaces:
Sleeping
Sleeping
Commit ·
4cdc4e2
1
Parent(s): 51b48a8
updated gradio to call the functions directly
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354import osimport gradio as grimport pandas as pdimport networkx as nximport matplotlib.pyplot as pltfrom Oracle.deepfundingoracle import prepare_dataset, train_predict_weight, create_submission_csv, SmolLMfrom Oracle.DataSmolAgent import DataSmolAgentdef pipeline(upload):# Load and clean/extract features via SmolAgentsdf_raw = pd.read_csv(upload.name)df_features = DataSmolAgent(df_raw).run(prompt="Clean and extract features from the uploaded data", output_csv=False)# Save preprocessed features and run dataset preparation on themprocessed_path = "processed_input.csv"pd.DataFrame(df_features).to_csv(processed_path, index=False)df_prepared = prepare_dataset(processed_path)# Assign base weights and predict final weightsdf_results = train_predict_weight(df_prepared)# Create submission CSVcsv_path = create_submission_csv(df_results, "submission.csv")# Build dependency graphG = nx.DiGraph()for _, row in df_results.iterrows():G.add_edge(row["parent"], row["repo"], weight=row["final_weight"])plt.figure(figsize=(10, 8))pos = nx.spring_layout(G)weights = [G[u][v]["weight"] for u, v in G.edges()]nx.draw(G, pos, with_labels=True, node_size=500, node_color="lightblue",edge_color=weights, edge_cmap=plt.get_cmap('viridis'), width=2)plt.savefig("graph.png")# Generate explanation via SmolLMexplanation = SmolLM().predict("Explain the dependency graph and weight assignments for the dataset.")# Return resultsreturn (df_results.head().to_dict("records"), csv_path, "graph.png", explanation)iface = gr.Interface(fn=pipeline,inputs=gr.File(label="Upload CSV", type="filepath"),outputs=[gr.Dataframe(label="Preview of Results"),gr.File(label="Download CSV"),gr.Image(label="Dependency Graph"),gr.Textbox(label="Explanation")],title="DeepFunding Oracle",description=("Upload a CSV to extract features, assign base weights via LLama, predict final weights with RandomForest, ""and visualize the dependency graph with explanations."))if __name__ == "__main__":1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950import osimport gradio as grfrom Oracle.deepfundingoracle import prepare_dataset, train_predict_weight, create_submission_csvimport pandas as pdimport matplotlib.pyplot as pltimport timeimport iodef analyze_file(file, progress=gr.Progress(track_tqdm=True)):start_time = time.time()progress(0, desc="Preparing dataset...")df = prepare_dataset(file.name)progress(0.3, desc="Predicting weights...")df = train_predict_weight(df)progress(0.6, desc="Saving results to CSV...")csv_path = create_submission_csv(df, "submission.csv")progress(0.8, desc="Generating graph...")# Example: plot histogram of a column if existsfig, ax = plt.subplots()if 'final_weight' in df.columns:df['final_weight'].hist(ax=ax)ax.set_title('Distribution of Final Weights')ax.set_xlabel('Final Weight')ax.set_ylabel('Count')else:ax.text(0.5, 0.5, 'No final_weight column to plot', ha='center')buf = io.BytesIO()plt.savefig(buf, format='png')buf.seek(0)plt.close(fig)progress(1, desc="Done!")elapsed = time.time() - start_timepreview = df.head().to_csv(index=False)return preview, csv_path, buf, f"Analysis completed in {elapsed:.2f} seconds."iface = gr.Interface(fn=analyze_file,inputs=gr.File(label="Upload CSV"),outputs=[gr.Textbox(label="Preview of Results"),gr.File(label="Download CSV"),gr.Image(label="Analysis Graph"),gr.Textbox(label="Status/Timing Info")],title="DeepFunding Oracle",description="Upload a CSV of repo-parent relationships; see analysis progress, get a graph, and download results as CSV.",allow_flagging="never")if __name__ == "__main__":
app.py+36-40