Spaces:
Running
on
Zero
Running
on
Zero
Added model list to the UI
Browse files- app.py +47 -53
- app_old.py +2 -2
- phi3_instruct_graph.py → llm_graph.py +11 -3
- main.py +2 -2
app.py
CHANGED
|
@@ -7,7 +7,7 @@ import rapidjson
|
|
| 7 |
import gradio as gr
|
| 8 |
import networkx as nx
|
| 9 |
|
| 10 |
-
from
|
| 11 |
from pyvis.network import Network
|
| 12 |
from spacy import displacy
|
| 13 |
from spacy.tokens import Span
|
|
@@ -53,16 +53,16 @@ def handle_text(text=""):
|
|
| 53 |
return " ".join(text.split())
|
| 54 |
|
| 55 |
# @spaces.GPU
|
| 56 |
-
def extract_kg(text=""):
|
| 57 |
"""
|
| 58 |
Extract knowledge graph from text
|
| 59 |
"""
|
| 60 |
|
| 61 |
# Catch empty text
|
| 62 |
-
if not text:
|
| 63 |
-
raise gr.Error("⚠️
|
| 64 |
try:
|
| 65 |
-
model =
|
| 66 |
result = model.extract(text)
|
| 67 |
return rapidjson.loads(result)
|
| 68 |
except Exception as e:
|
|
@@ -175,8 +175,8 @@ def create_graph(json_data):
|
|
| 175 |
# Create network visualization
|
| 176 |
network = Network(
|
| 177 |
width="100%",
|
| 178 |
-
height="700px",
|
| 179 |
-
|
| 180 |
notebook=False,
|
| 181 |
bgcolor="#f8fafc",
|
| 182 |
font_color="#1e293b"
|
|
@@ -184,29 +184,29 @@ def create_graph(json_data):
|
|
| 184 |
|
| 185 |
# Configure network display
|
| 186 |
network.from_nx(G)
|
| 187 |
-
network.barnes_hut(
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
)
|
| 195 |
-
|
| 196 |
-
# Customize edge appearance
|
| 197 |
-
for edge in network.edges:
|
| 198 |
-
edge['width'] = 2
|
| 199 |
-
edge['arrows'] = {'to': {'enabled': False, 'type': 'arrow'}}
|
| 200 |
-
edge['color'] = {'color': '#6366f1', 'highlight': '#4f46e5'}
|
| 201 |
-
edge['font'] = {'size': 12, 'color': '#4b5563', 'face': 'Arial'}
|
| 202 |
|
| 203 |
# Customize node appearance
|
| 204 |
for node in network.nodes:
|
| 205 |
node['color'] = {'background': '#e0e7ff', 'border': '#6366f1', 'highlight': {'background': '#c7d2fe', 'border': '#4f46e5'}}
|
| 206 |
node['font'] = {'size': 14, 'color': '#1e293b'}
|
| 207 |
node['shape'] = 'dot'
|
| 208 |
-
node['size'] =
|
| 209 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 210 |
# Generate HTML with iframe to isolate styles
|
| 211 |
html = network.generate_html()
|
| 212 |
html = html.replace("'", '"')
|
|
@@ -217,13 +217,13 @@ def create_graph(json_data):
|
|
| 217 |
allow-top-navigation-by-user-activation allow-downloads" allowfullscreen=""
|
| 218 |
allowpaymentrequest="" frameborder="0" srcdoc='{html}'></iframe>"""
|
| 219 |
|
| 220 |
-
def process_and_visualize(text, progress=gr.Progress()):
|
| 221 |
"""
|
| 222 |
Process text and visualize knowledge graph and entities
|
| 223 |
"""
|
| 224 |
|
| 225 |
-
if not text:
|
| 226 |
-
raise gr.Error("⚠️
|
| 227 |
|
| 228 |
# Check if we're processing the first example for caching
|
| 229 |
is_first_example = text == EXAMPLES[0][0]
|
|
@@ -242,7 +242,7 @@ def process_and_visualize(text, progress=gr.Progress()):
|
|
| 242 |
|
| 243 |
# Continue with normal processing if cache fails
|
| 244 |
progress(0, desc="Starting extraction...")
|
| 245 |
-
json_data = extract_kg(text)
|
| 246 |
|
| 247 |
progress(0.5, desc="Creating entity visualization...")
|
| 248 |
entities_viz = create_custom_entity_viz(json_data, text)
|
|
@@ -275,9 +275,9 @@ def process_and_visualize(text, progress=gr.Progress()):
|
|
| 275 |
EXAMPLES = [
|
| 276 |
[handle_text("""The family of Azerbaijan President Ilham Aliyev leads a charmed, glamorous life, thanks in part to financial interests in almost every sector of the economy.
|
| 277 |
His wife, Mehriban, comes from the privileged and powerful Pashayev family that owns banks, insurance and construction companies,
|
| 278 |
-
a television station and a line of cosmetics. She has led the Heydar Aliyev Foundation, Azerbaijan
|
| 279 |
-
hospitals and the country
|
| 280 |
-
have financial stakes in a firm that won rights to mine for gold in the western village of Chovdar and Azerfon, the country
|
| 281 |
Arzu is also a significant shareholder in SW Holding, which controls nearly every operation related to Azerbaijan Airlines (“Azal”), from meals to airport taxis.
|
| 282 |
Both sisters and brother Heydar own property in Dubai valued at roughly $75 million in 2010;
|
| 283 |
Heydar is the legal owner of nine luxury mansions in Dubai purchased for some $44 million.""")],
|
|
@@ -286,16 +286,11 @@ EXAMPLES = [
|
|
| 286 |
citing lead singer Steven Tyler's unrecoverable vocal cord injury.
|
| 287 |
The decision comes after months of unsuccessful treatment for Tyler's fractured larynx, which he suffered in September 2023.""")],
|
| 288 |
|
| 289 |
-
[handle_text("""
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
goûtent aux spécialités régionales comme la tapenade, les calissons d’Aix ou les délicieuses fougasses à l’huile d’olive.
|
| 295 |
-
Ces marchés ne sont pas seulement des lieux d’achat, mais de véritables scènes de vie où se transmettent les traditions
|
| 296 |
-
culinaires et l’art de vivre à la provençale. Entre deux achats, on s’attarde pour déguster un café serré ou un verre de rosé frais,
|
| 297 |
-
sous le chant des cigales, rappelant que la Provence est avant tout une terre de convivialité et de saveurs authentiques. Une balade dans ces marchés,
|
| 298 |
-
c’est un voyage sensoriel au cœur d’une culture où le temps semble suspendu, entre simplicité et raffinement.""")],
|
| 299 |
]
|
| 300 |
|
| 301 |
def generate_first_example_cache():
|
|
@@ -308,7 +303,7 @@ def generate_first_example_cache():
|
|
| 308 |
|
| 309 |
try:
|
| 310 |
text = EXAMPLES[0][0]
|
| 311 |
-
model =
|
| 312 |
|
| 313 |
# Extract data
|
| 314 |
json_data = extract_kg(text, model)
|
|
@@ -357,16 +352,16 @@ def create_ui():
|
|
| 357 |
gr.Markdown(f"# {TITLE}")
|
| 358 |
gr.Markdown(f"{SUBTITLE}")
|
| 359 |
|
| 360 |
-
# Main content area
|
| 361 |
with gr.Row():
|
| 362 |
# Left panel - Input controls
|
| 363 |
with gr.Column(scale=1):
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
|
| 371 |
input_text = gr.TextArea(
|
| 372 |
label="📝 Input Text",
|
|
@@ -409,8 +404,7 @@ def create_ui():
|
|
| 409 |
# Functionality
|
| 410 |
submit_button.click(
|
| 411 |
fn=process_and_visualize,
|
| 412 |
-
|
| 413 |
-
inputs=[input_text],
|
| 414 |
outputs=[output_graph, output_entity_viz, output_json, stats_output]
|
| 415 |
)
|
| 416 |
|
|
@@ -436,8 +430,8 @@ def create_ui():
|
|
| 436 |
|
| 437 |
# Footer
|
| 438 |
gr.Markdown("---")
|
| 439 |
-
gr.Markdown("📋 **Instructions:** Enter text in any language and click
|
| 440 |
-
gr.Markdown("🛠️ Powered by [Phi-3-mini-128k-instruct-graph](https://huggingface.co/EmergentMethods/Phi-3-mini-128k-instruct-graph)")
|
| 441 |
|
| 442 |
return demo
|
| 443 |
|
|
|
|
| 7 |
import gradio as gr
|
| 8 |
import networkx as nx
|
| 9 |
|
| 10 |
+
from llm_graph import LLMGraph, MODEL_LIST
|
| 11 |
from pyvis.network import Network
|
| 12 |
from spacy import displacy
|
| 13 |
from spacy.tokens import Span
|
|
|
|
| 53 |
return " ".join(text.split())
|
| 54 |
|
| 55 |
# @spaces.GPU
|
| 56 |
+
def extract_kg(text="", model=None):
|
| 57 |
"""
|
| 58 |
Extract knowledge graph from text
|
| 59 |
"""
|
| 60 |
|
| 61 |
# Catch empty text
|
| 62 |
+
if not text or not model:
|
| 63 |
+
raise gr.Error("⚠️ Both text and model must be provided!")
|
| 64 |
try:
|
| 65 |
+
model = LLMGraph(model=model)
|
| 66 |
result = model.extract(text)
|
| 67 |
return rapidjson.loads(result)
|
| 68 |
except Exception as e:
|
|
|
|
| 175 |
# Create network visualization
|
| 176 |
network = Network(
|
| 177 |
width="100%",
|
| 178 |
+
# height="700px",
|
| 179 |
+
height="100vh",
|
| 180 |
notebook=False,
|
| 181 |
bgcolor="#f8fafc",
|
| 182 |
font_color="#1e293b"
|
|
|
|
| 184 |
|
| 185 |
# Configure network display
|
| 186 |
network.from_nx(G)
|
| 187 |
+
# network.barnes_hut(
|
| 188 |
+
# gravity=-3000,
|
| 189 |
+
# central_gravity=0.3,
|
| 190 |
+
# spring_length=50,
|
| 191 |
+
# spring_strength=0.001,
|
| 192 |
+
# damping=0.09,
|
| 193 |
+
# overlap=0,
|
| 194 |
+
# )
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
|
| 196 |
# Customize node appearance
|
| 197 |
for node in network.nodes:
|
| 198 |
node['color'] = {'background': '#e0e7ff', 'border': '#6366f1', 'highlight': {'background': '#c7d2fe', 'border': '#4f46e5'}}
|
| 199 |
node['font'] = {'size': 14, 'color': '#1e293b'}
|
| 200 |
node['shape'] = 'dot'
|
| 201 |
+
node['size'] = 20
|
| 202 |
+
|
| 203 |
+
# Customize edge appearance
|
| 204 |
+
for edge in network.edges:
|
| 205 |
+
edge['width'] = 4
|
| 206 |
+
# edge['arrows'] = {'to': {'enabled': False, 'type': 'arrow'}}
|
| 207 |
+
edge['color'] = {'color': '#6366f1', 'highlight': '#4f46e5'}
|
| 208 |
+
edge['font'] = {'size': 12, 'color': '#4b5563', 'face': 'Arial'}
|
| 209 |
+
|
| 210 |
# Generate HTML with iframe to isolate styles
|
| 211 |
html = network.generate_html()
|
| 212 |
html = html.replace("'", '"')
|
|
|
|
| 217 |
allow-top-navigation-by-user-activation allow-downloads" allowfullscreen=""
|
| 218 |
allowpaymentrequest="" frameborder="0" srcdoc='{html}'></iframe>"""
|
| 219 |
|
| 220 |
+
def process_and_visualize(text, model, progress=gr.Progress()):
|
| 221 |
"""
|
| 222 |
Process text and visualize knowledge graph and entities
|
| 223 |
"""
|
| 224 |
|
| 225 |
+
if not text or not model:
|
| 226 |
+
raise gr.Error("⚠️ Both text and model must be provided!")
|
| 227 |
|
| 228 |
# Check if we're processing the first example for caching
|
| 229 |
is_first_example = text == EXAMPLES[0][0]
|
|
|
|
| 242 |
|
| 243 |
# Continue with normal processing if cache fails
|
| 244 |
progress(0, desc="Starting extraction...")
|
| 245 |
+
json_data = extract_kg(text, model)
|
| 246 |
|
| 247 |
progress(0.5, desc="Creating entity visualization...")
|
| 248 |
entities_viz = create_custom_entity_viz(json_data, text)
|
|
|
|
| 275 |
EXAMPLES = [
|
| 276 |
[handle_text("""The family of Azerbaijan President Ilham Aliyev leads a charmed, glamorous life, thanks in part to financial interests in almost every sector of the economy.
|
| 277 |
His wife, Mehriban, comes from the privileged and powerful Pashayev family that owns banks, insurance and construction companies,
|
| 278 |
+
a television station and a line of cosmetics. She has led the Heydar Aliyev Foundation, Azerbaijan's pre-eminent charity behind the construction of schools,
|
| 279 |
+
hospitals and the country's major sports complex. Their eldest daughter, Leyla, editor of Baku magazine, and her sister, Arzu,
|
| 280 |
+
have financial stakes in a firm that won rights to mine for gold in the western village of Chovdar and Azerfon, the country's largest mobile phone business.
|
| 281 |
Arzu is also a significant shareholder in SW Holding, which controls nearly every operation related to Azerbaijan Airlines (“Azal”), from meals to airport taxis.
|
| 282 |
Both sisters and brother Heydar own property in Dubai valued at roughly $75 million in 2010;
|
| 283 |
Heydar is the legal owner of nine luxury mansions in Dubai purchased for some $44 million.""")],
|
|
|
|
| 286 |
citing lead singer Steven Tyler's unrecoverable vocal cord injury.
|
| 287 |
The decision comes after months of unsuccessful treatment for Tyler's fractured larynx, which he suffered in September 2023.""")],
|
| 288 |
|
| 289 |
+
[handle_text("""Les jardins du Luxembourg, situés au cœur du sixième arrondissement de Paris, offrent un véritable havre de paix aux citadins pressés.
|
| 290 |
+
Créés au début du dix-septième siècle sur l'initiative de Marie de Médicis, ces jardins à la française s'étendent sur vingt-trois hectares
|
| 291 |
+
et abritent le Palais du Luxembourg, siège du Sénat français. Les promeneurs peuvent y admirer les parterres de fleurs soigneusement entretenus,
|
| 292 |
+
les bassins ornés de statues mythologiques, et les allées bordées de marronniers centenaires. Chaque matin, les jardiniers s'affairent à tailler
|
| 293 |
+
les buis et à arroser les rosiers, perpétuant ainsi une tradition d'excellence horticole qui fait la fierté de la capitale française.""")],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 294 |
]
|
| 295 |
|
| 296 |
def generate_first_example_cache():
|
|
|
|
| 303 |
|
| 304 |
try:
|
| 305 |
text = EXAMPLES[0][0]
|
| 306 |
+
model = MODEL_LIST[0] if MODEL_LIST else None
|
| 307 |
|
| 308 |
# Extract data
|
| 309 |
json_data = extract_kg(text, model)
|
|
|
|
| 352 |
gr.Markdown(f"# {TITLE}")
|
| 353 |
gr.Markdown(f"{SUBTITLE}")
|
| 354 |
|
| 355 |
+
# Main content area
|
| 356 |
with gr.Row():
|
| 357 |
# Left panel - Input controls
|
| 358 |
with gr.Column(scale=1):
|
| 359 |
+
input_model = gr.Dropdown(
|
| 360 |
+
MODEL_LIST,
|
| 361 |
+
label="🤖 Select Model",
|
| 362 |
+
info="Choose a model to process your text",
|
| 363 |
+
value=MODEL_LIST[0] if MODEL_LIST else None
|
| 364 |
+
)
|
| 365 |
|
| 366 |
input_text = gr.TextArea(
|
| 367 |
label="📝 Input Text",
|
|
|
|
| 404 |
# Functionality
|
| 405 |
submit_button.click(
|
| 406 |
fn=process_and_visualize,
|
| 407 |
+
inputs=[input_text, input_model],
|
|
|
|
| 408 |
outputs=[output_graph, output_entity_viz, output_json, stats_output]
|
| 409 |
)
|
| 410 |
|
|
|
|
| 430 |
|
| 431 |
# Footer
|
| 432 |
gr.Markdown("---")
|
| 433 |
+
gr.Markdown("📋 **Instructions:** Enter text in any language, select a model and click `Extract & Visualize` to generate a knowledge graph.")
|
| 434 |
+
gr.Markdown("🛠️ Powered by [GPT-4.1-mini](https://platform.openai.com/docs/models/gpt-4.1-mini) and [Phi-3-mini-128k-instruct-graph](https://huggingface.co/EmergentMethods/Phi-3-mini-128k-instruct-graph)")
|
| 435 |
|
| 436 |
return demo
|
| 437 |
|
app_old.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
# import spaces
|
| 2 |
import gradio as gr
|
| 3 |
-
from
|
| 4 |
import rapidjson
|
| 5 |
from pyvis.network import Network
|
| 6 |
import networkx as nx
|
|
@@ -43,7 +43,7 @@ def handle_text(text):
|
|
| 43 |
# @spaces.GPU
|
| 44 |
def extract(text, model):
|
| 45 |
try:
|
| 46 |
-
model =
|
| 47 |
result = model.extract(text)
|
| 48 |
return rapidjson.loads(result)
|
| 49 |
except Exception as e:
|
|
|
|
| 1 |
# import spaces
|
| 2 |
import gradio as gr
|
| 3 |
+
from llm_graph import MODEL_LIST, LLMGraph
|
| 4 |
import rapidjson
|
| 5 |
from pyvis.network import Network
|
| 6 |
import networkx as nx
|
|
|
|
| 43 |
# @spaces.GPU
|
| 44 |
def extract(text, model):
|
| 45 |
try:
|
| 46 |
+
model = LLMGraph(model=model)
|
| 47 |
result = model.extract(text)
|
| 48 |
return rapidjson.loads(result)
|
| 49 |
except Exception as e:
|
phi3_instruct_graph.py → llm_graph.py
RENAMED
|
@@ -14,12 +14,20 @@ client = InferenceClient(
|
|
| 14 |
token=api_token
|
| 15 |
)
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
"""
|
| 20 |
Initialize the Phi3InstructGraph with a specified model.
|
| 21 |
"""
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
| 23 |
self.model_path = model
|
| 24 |
|
| 25 |
def _generate(self, messages):
|
|
|
|
| 14 |
token=api_token
|
| 15 |
)
|
| 16 |
|
| 17 |
+
MODEL_LIST = [
|
| 18 |
+
"OpenAI/GPT-4.1-mini",
|
| 19 |
+
"EmergentMethods/Phi-3-mini-128k-instruct-graph",
|
| 20 |
+
]
|
| 21 |
+
|
| 22 |
+
class LLMGraph:
|
| 23 |
+
def __init__(self, model="OpenAI/GPT-4.1-mini"):
|
| 24 |
"""
|
| 25 |
Initialize the Phi3InstructGraph with a specified model.
|
| 26 |
"""
|
| 27 |
+
|
| 28 |
+
if model not in MODEL_LIST:
|
| 29 |
+
raise ValueError(f"Model must be one of {MODEL_LIST}")
|
| 30 |
+
|
| 31 |
self.model_path = model
|
| 32 |
|
| 33 |
def _generate(self, messages):
|
main.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
# import spaces
|
| 2 |
import gradio as gr
|
| 3 |
-
from
|
| 4 |
import rapidjson
|
| 5 |
from pyvis.network import Network
|
| 6 |
import networkx as nx
|
|
@@ -68,7 +68,7 @@ def handle_text(text):
|
|
| 68 |
# Core extraction function
|
| 69 |
# @spaces.GPU
|
| 70 |
def extract(text, model):
|
| 71 |
-
model =
|
| 72 |
try:
|
| 73 |
result = model.extract(text)
|
| 74 |
return rapidjson.loads(result)
|
|
|
|
| 1 |
# import spaces
|
| 2 |
import gradio as gr
|
| 3 |
+
from llm_graph import MODEL_LIST, LLMGraph
|
| 4 |
import rapidjson
|
| 5 |
from pyvis.network import Network
|
| 6 |
import networkx as nx
|
|
|
|
| 68 |
# Core extraction function
|
| 69 |
# @spaces.GPU
|
| 70 |
def extract(text, model):
|
| 71 |
+
model = LLMGraph(model=model)
|
| 72 |
try:
|
| 73 |
result = model.extract(text)
|
| 74 |
return rapidjson.loads(result)
|