Spaces:
Runtime error
Runtime error
update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,94 @@
|
|
| 1 |
import os
|
| 2 |
import json
|
|
|
|
|
|
|
|
|
|
| 3 |
import networkx as nx
|
|
|
|
|
|
|
| 4 |
import pyvis
|
| 5 |
-
from
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# Define a tool for analyzing the Florentine Families graph
|
| 8 |
@tool
|
|
@@ -32,14 +118,69 @@ def analyze_florentine_graph(metric: str) -> str:
|
|
| 32 |
analysis += f"- {node}: {value:.3f}\n"
|
| 33 |
return analysis
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
# Initialize the agent with proper configuration
|
| 36 |
model = HfApiModel()
|
| 37 |
agent = CodeAgent(
|
| 38 |
-
tools=[analyze_florentine_graph],
|
| 39 |
model=model,
|
| 40 |
-
additional_authorized_imports=["networkx","pyvis","
|
| 41 |
add_base_tools=True
|
| 42 |
)
|
| 43 |
|
| 44 |
-
#
|
| 45 |
-
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import json
|
| 3 |
+
from typing import Optional
|
| 4 |
+
import gradio as gr
|
| 5 |
+
from gradio import Interface, Blocks
|
| 6 |
import networkx as nx
|
| 7 |
+
from networkx import erdos_renyi_graph
|
| 8 |
+
import community as community_louvain
|
| 9 |
import pyvis
|
| 10 |
+
from pyvis.network import Network
|
| 11 |
+
from smolagents import CodeAgent, HfApiModel, tool
|
| 12 |
+
from opentelemetry import trace
|
| 13 |
+
from opentelemetry.sdk.trace import TracerProvider
|
| 14 |
+
from opentelemetry.sdk.trace.export import BatchSpanProcessor
|
| 15 |
+
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
|
| 16 |
+
from openinference.instrumentation.smolagents import SmolagentsInstrumentor
|
| 17 |
+
|
| 18 |
+
# set HF_TOKEN environment variable
|
| 19 |
+
os.environ["HF_TOKEN"] = os.getenv("HF_TOKEN")
|
| 20 |
+
|
| 21 |
+
# Set up telemetry
|
| 22 |
+
PHOENIX_API_KEY = os.getenv("PHOENIX_API_KEY")
|
| 23 |
+
api_key = f"api_key={PHOENIX_API_KEY}"
|
| 24 |
+
os.environ["OTEL_EXPORTER_OTLP_HEADERS"] = api_key
|
| 25 |
+
os.environ["PHOENIX_CLIENT_HEADERS"] = api_key
|
| 26 |
+
os.environ["PHOENIX_COLLECTOR_ENDPOINT"] = "https://app.phoenix.arize.com"
|
| 27 |
+
|
| 28 |
+
# Updated endpoint from local to cloud
|
| 29 |
+
endpoint = "https://app.phoenix.arize.com/v1/traces"
|
| 30 |
+
trace_provider = TracerProvider()
|
| 31 |
+
trace_provider.add_span_processor(BatchSpanProcessor(OTLPSpanExporter(endpoint)))
|
| 32 |
+
SmolagentsInstrumentor().instrument(tracer_provider=trace_provider)
|
| 33 |
+
|
| 34 |
+
# Define examples
|
| 35 |
+
examples = [
|
| 36 |
+
["Highlight the Medici family's central role in the network using all three centrality metrics."],
|
| 37 |
+
["Focus on the Strozzi family's role in the network using betweenness centrality."],
|
| 38 |
+
["Compare the Albizzi and Medici families' positions in the network using degree and closeness centrality."],
|
| 39 |
+
["Visualize the overall network structure and the relative positions of the families."],
|
| 40 |
+
["Explore the roles of other families with significant centrality values."]
|
| 41 |
+
]
|
| 42 |
+
|
| 43 |
+
from smolagents import GradioUI
|
| 44 |
+
import gradio as gr
|
| 45 |
+
|
| 46 |
+
from smolagents import GradioUI
|
| 47 |
+
import gradio as gr
|
| 48 |
+
|
| 49 |
+
class GradioUIWithExamples(GradioUI):
|
| 50 |
+
def __init__(self, agent, examples=None, **kwargs):
|
| 51 |
+
super().__init__(agent, **kwargs)
|
| 52 |
+
self.examples = examples
|
| 53 |
+
|
| 54 |
+
def build_interface(self):
|
| 55 |
+
with gr.Blocks() as demo:
|
| 56 |
+
gr.Markdown("## Florentine Families Network Analysis")
|
| 57 |
+
|
| 58 |
+
# Main Input/Output
|
| 59 |
+
input_box = gr.Textbox(
|
| 60 |
+
label="Your Question",
|
| 61 |
+
placeholder="Type your question about the Florentine Families graph...",
|
| 62 |
+
)
|
| 63 |
+
output_box = gr.Textbox(
|
| 64 |
+
label="Agent's Response",
|
| 65 |
+
placeholder="Response will appear here...",
|
| 66 |
+
interactive=False,
|
| 67 |
+
)
|
| 68 |
+
submit_button = gr.Button("Submit")
|
| 69 |
+
|
| 70 |
+
# Link submit button to agent logic
|
| 71 |
+
submit_button.click(
|
| 72 |
+
self.agent.run,
|
| 73 |
+
inputs=input_box,
|
| 74 |
+
outputs=output_box,
|
| 75 |
+
)
|
| 76 |
+
|
| 77 |
+
# Add Examples
|
| 78 |
+
if self.examples:
|
| 79 |
+
gr.Markdown("### Examples")
|
| 80 |
+
for example in self.examples:
|
| 81 |
+
gr.Button(example[0]).click(
|
| 82 |
+
lambda x=example[0]: x, # Populate input box
|
| 83 |
+
inputs=[],
|
| 84 |
+
outputs=input_box,
|
| 85 |
+
)
|
| 86 |
+
return demo
|
| 87 |
+
|
| 88 |
+
def launch(self):
|
| 89 |
+
# Use the custom-built interface instead of the base class's logic
|
| 90 |
+
demo = self.build_interface()
|
| 91 |
+
demo.launch()
|
| 92 |
|
| 93 |
# Define a tool for analyzing the Florentine Families graph
|
| 94 |
@tool
|
|
|
|
| 118 |
analysis += f"- {node}: {value:.3f}\n"
|
| 119 |
return analysis
|
| 120 |
|
| 121 |
+
from smolagents import tool
|
| 122 |
+
|
| 123 |
+
@tool
|
| 124 |
+
def save_html_to_file(html_content: str, file_path: str) -> str:
|
| 125 |
+
"""
|
| 126 |
+
Saves the provided HTML content to a file.
|
| 127 |
+
|
| 128 |
+
Args:
|
| 129 |
+
html_content: The HTML content to save.
|
| 130 |
+
file_path: The path where the HTML file will be saved.
|
| 131 |
+
|
| 132 |
+
Returns:
|
| 133 |
+
A confirmation message upon successful saving.
|
| 134 |
+
"""
|
| 135 |
+
with open(file_path, 'w', encoding='utf-8') as file:
|
| 136 |
+
file.write(html_content)
|
| 137 |
+
return f"HTML content successfully saved to {file_path}"
|
| 138 |
+
|
| 139 |
+
from smolagents import tool
|
| 140 |
+
|
| 141 |
+
@tool
|
| 142 |
+
def read_html_from_file(file_path: str) -> str:
|
| 143 |
+
"""
|
| 144 |
+
Reads HTML content from a file.
|
| 145 |
+
|
| 146 |
+
Args:
|
| 147 |
+
file_path: The path of the HTML file to read.
|
| 148 |
+
|
| 149 |
+
Returns:
|
| 150 |
+
The HTML content as a string.
|
| 151 |
+
"""
|
| 152 |
+
with open(file_path, 'r', encoding='utf-8') as file:
|
| 153 |
+
html_content = file.read()
|
| 154 |
+
return html_content
|
| 155 |
+
|
| 156 |
+
from smolagents import tool
|
| 157 |
+
import networkx as nx
|
| 158 |
+
import json
|
| 159 |
+
|
| 160 |
+
@tool
|
| 161 |
+
def export_graph_to_json(graph_data: dict) -> str:
|
| 162 |
+
"""
|
| 163 |
+
Exports a NetworkX graph represented as a dictionary to JSON format.
|
| 164 |
+
|
| 165 |
+
Args:
|
| 166 |
+
graph_data: The graph data in node-link format.
|
| 167 |
+
|
| 168 |
+
Returns:
|
| 169 |
+
str: The JSON representation of the graph.
|
| 170 |
+
"""
|
| 171 |
+
graph = nx.node_link_graph(graph_data)
|
| 172 |
+
json_output = json.dumps(nx.node_link_data(graph), indent=4)
|
| 173 |
+
return json_output
|
| 174 |
+
|
| 175 |
# Initialize the agent with proper configuration
|
| 176 |
model = HfApiModel()
|
| 177 |
agent = CodeAgent(
|
| 178 |
+
tools=[analyze_florentine_graph, save_html_to_file, read_html_from_file, export_graph_to_json],
|
| 179 |
model=model,
|
| 180 |
+
additional_authorized_imports=["gradio","networkx","community_louvain","pyvis","matplotlib","json"],
|
| 181 |
add_base_tools=True
|
| 182 |
)
|
| 183 |
|
| 184 |
+
# Use the fixed GradioUIWithExamples
|
| 185 |
+
interface = GradioUIWithExamples(agent, examples=examples)
|
| 186 |
+
interface.launch()
|