Spaces:
Running on Zero
Running on Zero
Multiple updates
Browse filesFiles changed (4)
- README.md+2-2
- app.py+117-68
- app-backup.py → app_old.pyRenamed
- phi3_instruct_graph.py+32-24
12323456---title: Graph Mindemoji: 👀title: Text2Graphemoji: 🌐colorFrom: purplecolorTo: pinksdk: gradio123456789104116712131011121415151616171819209 unmodified lines3031323233343536373839404142433940444546474849505152535443555645575859606162636465476667686951707172737475767778557980575881828384858663878865899091923 unmodified lines9697987599100101102103104105106107108109821101111121131141158788891161171181191201211221231241251261271281291301311321331031341351364 unmodified lines1411421431141441451461471481491501511521531541551561571231581591601261611621631281641651661311671681691703 unmodified lines1741751761411771781791441801811821831841851861511521871881891901913 unmodified lines1951961971621981991642002012022032041692052062072082092102111762122132142153 unmodified lines21922022122222322422522622722811 unmodified lines24024124220320424324424520624624724824922 unmodified lines2722732742352752762372382392402412422772782792802812822832842852862872882892902452462472912922932942502952522962972982993003013023033042572582593053063072613083093103112 unmodified lines3143153162703173183193203213223233242772783253263272803283293303317 unmodified lines3393403413423433443453463473482 unmodified lines35135235330230330435435535673 unmodified lines430431432384385433434435436437438390439# import spacesimport osimport spacyimport pickleimport randomimport rapidjsonimport gradio as grimport networkx as nx
from phi3_instruct_graph import Phi3InstructGraphimport rapidjsonfrom pyvis.network import Networkimport networkx as nximport spacyfrom spacy import displacyfrom spacy.tokens import Spanimport randomimport osimport pickle
# ConstantsTITLE = "🌐 GraphMind: Phi-3 Instruct Graph Explorer"SUBTITLE = "✨ Extract and visualize knowledge graphs from any text in multiple languages"TITLE = "🌐 Text2Graph: Extract Knowledge Graphs from Natural Language"SUBTITLE = "✨ Extract and visualize knowledge graphs from texts in any language!"
# Basic CSS for stylingCUSTOM_CSS = """9 unmodified lines# Create cache directory if it doesn't existos.makedirs(CACHE_DIR, exist_ok=True)
# Color utilitiesdef get_random_light_color(): """ Color utilities """
r = random.randint(140, 255) g = random.randint(140, 255) b = random.randint(140, 255)
return f"#{r:02x}{g:02x}{b:02x}"
# Text preprocessingdef handle_text(text):def handle_text(text=""): """ Text preprocessing """
# Catch empty text if not text: return "" return " ".join(text.split())
# Main processing functions# # @spaces.GPUdef extract(text):def extract_kg(text=""): """ Extract knowledge graph from text """
# Catch empty text if not text: raise gr.Error("⚠️ Text must be provided!") try: model = Phi3InstructGraph() model = Phi3InstructGraph() result = model.extract(text) return rapidjson.loads(result) except Exception as e: raise gr.Error(f"Extraction error: {str(e)}") raise gr.Error(f"❌ Extraction error: {str(e)}")
def find_token_indices(doc, substring, text): """ Find token indices for a given substring in the text based on the provided spaCy doc. """
result = [] start_index = text.find(substring) start_idx = text.find(substring) while start_index != -1: end_index = start_index + len(substring) while start_idx != -1: end_idx = start_idx + len(substring) start_token = None end_token = None
for token in doc: if token.idx == start_index: if token.idx == start_idx: start_token = token.i if token.idx + len(token) == end_index: if token.idx + len(token) == end_idx: end_token = token.i + 1
if start_token is not None and end_token is not None:3 unmodified lines }) # Search for next occurrence start_index = text.find(substring, end_index) start_idx = text.find(substring, end_idx)
return result
def create_custom_entity_viz(data, full_text): """ Create custom entity visualization using spaCy's displacy """
nlp = spacy.blank("xx") doc = nlp(full_text)
spans = [] colors = {}
for node in data["nodes"]: entity_spans = find_token_indices(doc, node["id"], full_text)
for dataentity in entity_spans: start = dataentity["start"] end = dataentity["end"] for entity in entity_spans: start = entity["start"] end = entity["end"] if start < len(doc) and end <= len(doc): # Check for overlapping spans overlapping = any(s.start < end and start < s.end for s in spans)
if not overlapping: node_type = node.get("type", "Entity") span = Span(doc, start, end, label=node_type) spans.append(span)
if node_type not in colors: colors[node_type] = get_random_light_color()
doc.set_ents(spans, default="unmodified") doc.spans["sc"] = spans
options = { "colors": colors, "ents": list(colors.keys()),4 unmodified lines html = displacy.render(doc, style="span", options=options) # Add custom styling to the entity visualization styled_html = f""" <div style="padding: 20px; border-radius: 12px; background-color: white; box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);"> <div style="padding: 20px; border-radius: 12px; background-color: gray; box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);"> {html} </div> """
return styled_html
def create_graph(json_data): """ Create interactive knowledge graph using pyvis """
G = nx.Graph()
# Add nodes with tooltips - with error handling for missing keys # Add nodes with tooltips and error handling for missing keys for node in json_data['nodes']: # Get node type with fallback node_type = node.get("type", "Entity") type = node.get("type", "Entity")
# Get detailed type with fallback detailed_type = node.get("detailed_type", node_type) detailed_type = node.get("detailed_type", type) # Use node ID and type info for the tooltip G.add_node(node['id'], title=f"{node_type}: {detailed_type}") G.add_node(node['id'], title=f"{type}: {detailed_type}")
# Add edges with labels for edge in json_data['edges']:3 unmodified lines G.add_edge(edge['from'], edge['to'], title=label, label=label)
# Create network visualization nt = Network( network = Network( width="100%", height="700px", directed=True, directed=False, notebook=False, bgcolor="#f8fafc", font_color="#1e293b" ) # Configure network display nt.from_nx(G) nt.barnes_hut( network.from_nx(G) network.barnes_hut( gravity=-3000, central_gravity=0.3, spring_length=50,3 unmodified lines ) # Customize edge appearance for edge in nt.edges: for edge in network.edges: edge['width'] = 2 edge['arrows'] = {'to': {'enabled': True, 'type': 'arrow'}} edge['arrows'] = {'to': {'enabled': False, 'type': 'arrow'}} edge['color'] = {'color': '#6366f1', 'highlight': '#4f46e5'} edge['font'] = {'size': 12, 'color': '#4b5563', 'face': 'Arial'}
# Customize node appearance for node in nt.nodes: for node in network.nodes: node['color'] = {'background': '#e0e7ff', 'border': '#6366f1', 'highlight': {'background': '#c7d2fe', 'border': '#4f46e5'}} node['font'] = {'size': 14, 'color': '#1e293b'} node['shape'] = 'dot' node['size'] = 25
# Generate HTML with iframe to isolate styles html = nt.generate_html() html = network.generate_html() html = html.replace("'", '"')
return f"""<iframe style="width: 100%; height: 700px; margin: 0 auto; border-radius: 12px; box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -4px rgba(0, 0, 0, 0.1);" 3 unmodified lines allowpaymentrequest="" frameborder="0" srcdoc='{html}'></iframe>"""
def process_and_visualize(text, progress=gr.Progress()): """ Process text and visualize knowledge graph and entities """
if not text: raise gr.Error("⚠️ Text must be provided!") 11 unmodified lines return cache_data["graph_html"], cache_data["entities_viz"], cache_data["json_data"], cache_data["stats"] except Exception as e: print(f"Cache loading error: {str(e)}") # Continue with normal processing if cache fails
# Continue with normal processing if cache fails progress(0, desc="Starting extraction...") json_data = extract(text) json_data = extract_kg(text) progress(0.5, desc="Creating entity visualization...") entities_viz = create_custom_entity_viz(json_data, text)22 unmodified lines progress(1.0, desc="Complete!") return graph_html, entities_viz, json_data, stats
# Example texts in different languages# Example textsEXAMPLES = [ [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. His wife, Mehriban, comes from the privileged and powerful Pashayev family that owns banks, insurance and construction companies, 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, hospitals and the country’s major sports complex. Their eldest daughter, Leyla, editor of Baku magazine, and her sister, Arzu, 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. Arzu is also a significant shareholder in SW Holding, which controls nearly every operation related to Azerbaijan Airlines (“Azal”), from meals to airport taxis. Both sisters and brother Heydar own property in Dubai valued at roughly $75 million in 2010; Heydar is the legal owner of nine luxury mansions in Dubai purchased for some $44 million.""")], [handle_text("""Legendary rock band Aerosmith has officially announced their retirement from touring after 54 years, citing lead singer Steven Tyler's unrecoverable vocal cord injury. The decision comes after months of unsuccessful treatment for Tyler's fractured larynx, which he suffered in September 2023.""")], [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. His wife, Mehriban, comes from the privileged and powerful Pashayev family that owns banks, insurance and construction companies, 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, hospitals and the country’s major sports complex. Their eldest daughter, Leyla, editor of Baku magazine, and her sister, Arzu, 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. Arzu is also a significant shareholder in SW Holding, which controls nearly every operation related to Azerbaijan Airlines (“Azal”), from meals to airport taxis. Both sisters and brother Heydar own property in Dubai valued at roughly $75 million in 2010; Heydar is the legal owner of nine luxury mansions in Dubai purchased for some $44 million.""")],
[handle_text("""Legendary rock band Aerosmith has officially announced their retirement from touring after 54 years, citing lead singer Steven Tyler's unrecoverable vocal cord injury. The decision comes after months of unsuccessful treatment for Tyler's fractured larynx, which he suffered in September 2023.""")], [handle_text("""Pop star Justin Timberlake, 43, had his driver's license suspended by a New York judge during a virtual court hearing on August 2, 2024. The suspension follows Timberlake's arrest for driving while intoxicated (DWI) in Sag Harbor on June 18. Timberlake, who is currently on tour in Europe, pleaded not guilty to the charges.""")], court hearing on August 2, 2024. The suspension follows Timberlake's arrest for driving while intoxicated (DWI) in Sag Harbor on June 18. Timberlake, who is currently on tour in Europe, pleaded not guilty to the charges.""")],]
# Function to preprocess the first example when the app startsdef generate_first_example_cache(): """Generate cache for the first example if it doesn't exist""" """ Generate cache for the first example if it doesn't exist when the app starts """
if not os.path.exists(EXAMPLE_CACHE_FILE): print("Generating cache for first example...")
try: text = EXAMPLES[0][0] # model = MODEL_LIST[0] if MODEL_LIST else None # if model: model = Phi3InstructGraph()
# Extract data json_data = extract(text, model) json_data = extract_kg(text, model) entities_viz = create_custom_entity_viz(json_data, text) graph_html = create_graph(json_data) 2 unmodified lines stats = f"📊 Extracted {node_count} entities and {edge_count} relationships" # Save to cache cache_data = { cached_data = { "graph_html": graph_html, "entities_viz": entities_viz, "json_data": json_data, "stats": stats }
with open(EXAMPLE_CACHE_FILE, 'wb') as f: pickle.dump(cache_data, f) pickle.dump(cached_data, f) print("First example cache generated successfully")
return cache_data return cached_data except Exception as e: print(f"Error generating first example cache: {str(e)}") else:7 unmodified lines return None
def create_ui(): """ Create the Gradio UI """
# Try to generate/load the first example cache first_example_cache = generate_first_example_cache() 2 unmodified lines gr.Markdown(f"# {TITLE}") gr.Markdown(f"{SUBTITLE}") with gr.Row(): gr.Markdown("🌍 **Multilingual Support Available**") # Main content area - redesigned layout with gr.Row(): # Left panel - Input controls73 unmodified lines # Footer gr.Markdown("---") gr.Markdown("📋 **Instructions:** Enter text in any language, select a model, and click 'Extract & Visualize' to generate a knowledge graph.") gr.Markdown("🛠️ Powered by Phi-3 Instruct Graph | Emergent Methods") gr.Markdown("📋 **Instructions:** Enter text in any language and click 'Extract & Visualize' to generate a knowledge graph.") gr.Markdown("🛠️ Powered by [Phi-3-mini-128k-instruct-graph](https://huggingface.co/EmergentMethods/Phi-3-mini-128k-instruct-graph)") return demo
demo = create_ui()demo.launch(share=False)demo.launch(share=False)15 unmodified lines1617181920211922232425262728293031325 unmodified lines3839404142434445464729 unmodified lines7778796970717273747576777879808182838485868788899080818283848586878889909192939495969798991001011029510310415 unmodified lines
class Phi3InstructGraph: def __init__(self, model = "EmergentMethods/Phi-3-mini-4k-instruct-graph"): """ Initialize the Phi3InstructGraph with a specified model. """
self.model_path = model
def _generate(self, messages): """ Generate a response from the model based on the provided messages. """
# Use the chat_completion method response = client.chat_completion( messages=messages,5 unmodified lines return generated_text
def _get_messages(self, text): """ Construct the message list for the chat model. """ context = dedent("""\n A chat between a curious user and an artificial intelligence Assistant. The Assistant is an expert at identifying entities and relationships in text. The Assistant responds in JSON output only.
29 unmodified lines -------Text end------- """) if self.model_path == "EmergentMethods/Phi-3-medium-128k-instruct-graph": # model without system message -- why?? messages = [ { "role": "user", "content": f"{context}\n Input: {user_message}", } ] return messages else: messages = [ { "role": "system", "content": context }, { "role": "user", "content": user_message } ] return messages # if self.model_path == "EmergentMethods/Phi-3-medium-128k-instruct-graph": messages = [ { "role": "system", "content": context }, { "role": "user", "content": user_message } ] # else: # # TODO: update for other models
return messages def extract(self, text): """ Extract knowledge graph from text """
messages = self._get_messages(text) generated_text = self._generate(messages) # return pipe_output[0]["generated_text"]
return generated_textREADME.md+2-2
app.py+117-68
app-backup.py → app_old.pyRenamed
phi3_instruct_graph.py+32-24