Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files123446567891011121314151617181920212223242526272829301415161718193132333423242526272829303132333435363738394041424344454610 unmodified lines575859515253606162632 unmodified lines66676862636469706771727311 unmodified lines858687858688899011 unmodified lines1021031041041051061071081091101111121131141151161171181191201211191201211221221231241251261271281293 unmodified lines133134135133136137135136137138139140141138139140141142143144145146147148149150151152153154145146155156157158159150160152161162154163164165166167168169170171172159160161162163164165166167168169170171172173174175176177178179180181182183184185186187173189190191192193194195196197174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273import spacesimport gradio as grfrom phi3_instruct_graph import MODEL_LIST, Phi3InstructGraphfrom textwrap import dedentimport rapidjsonimport spacesfrom pyvis.network import Networkimport networkx as nximport spacyfrom spacy import displacyfrom spacy.tokens import Spanimport randomfrom tqdm import tqdm
# ConstantsTITLE = "π Phi-3 Instruct Graph Explorer"SUBTITLE = "β¨ Extract and visualize knowledge graphs from any text in multiple languages"THEME = gr.themes.Base().set( primary_hue="indigo", secondary_hue="purple", neutral_hue="slate", radius_size=gr.themes.sizes.radius_sm, shadow_size=gr.themes.sizes.shadow_lg,)
# Color utilitiesdef get_random_light_color(): r = random.randint(140, 255) g = random.randint(140, 255) b = random.randint(140, 255) return f"#{r:02x}{g:02x}{b:02x}"
@spaces.GPUdef extract(text, model): model = Phi3InstructGraph(model=model) result = model.extract(text) return rapidjson.loads(result)
# Text preprocessingdef handle_text(text): return " ".join(text.split())
def get_random_color(): return f"#{random.randint(0, 0xFFFFFF):06x}"
def get_random_light_color(): # Generate higher RGB values to ensure a lighter color r = random.randint(128, 255) g = random.randint(128, 255) b = random.randint(128, 255) return f"#{r:02x}{g:02x}{b:02x}"
def get_random_color(): return f"#{random.randint(0, 0xFFFFFF):06x}"# Main processing functions@spaces.GPUdef extract(text, model): try: model = Phi3InstructGraph(model=model) result = model.extract(text) return rapidjson.loads(result) except Exception as e: raise gr.Error(f"Extraction error: {str(e)}")
def find_token_indices(doc, substring, text): result = []10 unmodified lines if token.idx + len(token) == end_index: end_token = token.i + 1
if start_token is None or end_token is None: print(f"Token boundaries not found for '{substring}' at index {start_index}") else: if start_token is not None and end_token is not None: result.append({ "start": start_token, "end": end_token2 unmodified lines # Search for next occurrence start_index = text.find(substring, end_index)
if not result: print(f"Token boundaries not found for '{substring}'")
return result
def create_custom_entity_viz(data, full_text): nlp = spacy.blank("xx") doc = nlp(full_text)11 unmodified lines overlapping = any(s.start < end and start < s.end for s in spans) if not overlapping: span = Span(doc, start, end, label=node["type"]) # print(span) spans.append(span) if node["type"] not in colors: colors[node["type"]] = get_random_light_color()11 unmodified lines html = displacy.render(doc, style="span", options=options) return html
def create_graph(json_data): G = nx.Graph()
# Add nodes with tooltips for node in json_data['nodes']: G.add_node(node['id'], title=f"{node['type']}: {node['detailed_type']}")
# Add edges with labels for edge in json_data['edges']: G.add_edge(edge['from'], edge['to'], title=edge['label'], label=edge['label'])
# Create network visualization nt = Network( width="720px", height="600px", directed=True, notebook=False, bgcolor="#111827", font_color="white" # bgcolor="#FFFFFF", # font_color="#111827" bgcolor="#f8fafc", font_color="#1e293b" ) # Configure network display nt.from_nx(G) nt.barnes_hut( gravity=-3000,3 unmodified lines damping=0.09, overlap=0, )
# Customize edge appearance # for edge in nt.edges: # edge['font'] = {'size': 12, 'color': '#FFD700', 'face': 'Arial'} # Removed strokeWidth # edge['color'] = {'color': '#FF4500', 'highlight': '#FF4500'} # edge['width'] = 1 # edge['arrows'] = {'to': {'enabled': True, 'type': 'arrow'}} # edge['smooth'] = {'type': 'curvedCW', 'roundness': 0.2}
for edge in nt.edges: edge['width'] = 2 edge['arrows'] = {'to': {'enabled': True, 'type': 'arrow'}} edge['color'] = {'color': '#6366f1', 'highlight': '#4f46e5'} edge['font'] = {'size': 12, 'color': '#4b5563', 'face': 'Arial'}
# Customize node appearance for node in nt.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 = html.replace("'", '"')
return f"""<iframe style="width: 140%; height: 620px; margin: 0 auto;" name="result" allow="midi; geolocation; microphone; camera; display-capture; encrypted-media;" return f"""<iframe style="width: 100%; height: 620px; margin: 0 auto; border-radius: 8px; box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);" name="result" allow="midi; geolocation; microphone; camera; display-capture; encrypted-media;" sandbox="allow-modals allow-forms allow-scripts allow-same-origin allow-popups allow-top-navigation-by-user-activation allow-downloads" allowfullscreen="" allowpaymentrequest="" frameborder="0" srcdoc='{html}'></iframe>"""
def process_and_visualize(text, model):def process_and_visualize(text, model, progress=gr.Progress()): if not text or not model: raise gr.Error("Text and model must be provided.") raise gr.Error("β οΈ Both text and model must be provided.") progress(0, desc="Starting extraction...") json_data = extract(text, model) progress(0.5, desc="Creating entity visualization...") entities_viz = create_custom_entity_viz(json_data, text) progress(0.8, desc="Building knowledge graph...") graph_html = create_graph(json_data) return graph_html, entities_viz, json_data
with gr.Blocks(title="Phi-3 Instruct Graph (by Emergent Methods") as demo: gr.Markdown("# Phi-3 Instruct Graph (by Emergent Methods)") gr.Markdown("Extract a JSON graph from a text input and visualize it.") with gr.Row(): with gr.Column(scale=1): input_model = gr.Dropdown( MODEL_LIST, label="Model", ) input_text = gr.TextArea(label="Text", info="The text to be extracted")
examples = gr.Examples( examples=[ 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."""), ], inputs=input_text )
submit_button = gr.Button("Extract and Visualize") with gr.Column(scale=1): output_entity_viz = gr.HTML(label="Entities Visualization", show_label=True) output_graph = gr.HTML(label="Graph Visualization", show_label=True)
submit_button.click( fn=process_and_visualize, inputs=[input_text, input_model], outputs=[output_graph, output_entity_viz] ) node_count = len(json_data["nodes"]) edge_count = len(json_data["edges"]) stats = f"π Extracted {node_count} entities and {edge_count} relationships" progress(1.0, desc="Complete!") return graph_html, entities_viz, json_data, stats
# Example texts in different languagesEXAMPLES = [ [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.""")], [handle_text("""μΈκ³μ μΈ κΈ°μ κΈ°μ
μΌμ±μ μλ μλ‘μ΄ μΈκ³΅μ§λ₯ κΈ°λ° μ€λ§νΈν°μ μ¬ν΄ νλ°κΈ°μ μΆμν μμ μ΄λΌκ³ λ°ννλ€. μ΄ μ€λ§νΈν°μ νμ¬ κ°λ° μ€μΈ κ°€λμ μ리μ¦μ μ΅μ μμΌλ‘, κ°λ ₯ν AI κΈ°λ₯κ³Ό νμ μ μΈ μΉ΄λ©λΌ μμ€ν
μ νμ¬ν κ²μΌλ‘ μλ €μ‘λ€. μΌμ±μ μμ CEOλ μ΄λ² μ μ νμ΄ μ€λ§νΈν° μμ₯μ μλ‘μ΄ νμ μ κ°μ Έμ¬ κ²μ΄λΌκ³ μ λ§νλ€.""")], [handle_text("""νκ΅ μν 'κΈ°μμΆ©'μ 2020λ
μμΉ΄λ°λ―Έ μμμμμ μνμ, κ°λ
μ, κ°λ³Έμ, κ΅μ μνμ λ± 4κ° λΆλ¬Έμ μμνλ©° μμ¬λ₯Ό μλ‘ μΌλ€. λ΄μ€νΈ κ°λ
μ΄ μ°μΆν μ΄ μνλ νκ΅ μν μ΅μ΄λ‘ μΉΈ μνμ ν©κΈμ’
λ €μλ μμνμΌλ©°, μ μΈκ³μ μΌλ‘ μμ²λ ν₯νκ³Ό νλ¨μ νΈνμ λ°μλ€.""")]]
def create_ui(): with gr.Blocks(theme=THEME, title=TITLE) as demo: # Header gr.Markdown(f"# {TITLE}") gr.Markdown(f"{SUBTITLE}") with gr.Row(): gr.Markdown("π **Multilingual Support Available** π€") # Main interface with gr.Row(): # Input column with gr.Column(scale=1): input_model = gr.Dropdown( MODEL_LIST, label="π€ Select Model", info="Choose a model to process your text", value=MODEL_LIST[0] if MODEL_LIST else None ) input_text = gr.TextArea( label="π Input Text", info="Enter text in any language to extract a knowledge graph", placeholder="Enter text here...", lines=10 ) with gr.Row(): submit_button = gr.Button("π Extract & Visualize", variant="primary", scale=2) clear_button = gr.Button("π Clear", variant="secondary", scale=1) gr.Examples( examples=EXAMPLES, inputs=input_text, label="π Example Texts (English & Korean)" ) stats_output = gr.Markdown("", label="π Analysis Results") # Output column with gr.Column(scale=1): with gr.Tab("π§© Knowledge Graph"): output_graph = gr.HTML(label="") with gr.Tab("π·οΈ Entities"): output_entity_viz = gr.HTML(label="") with gr.Tab("π JSON Data"): output_json = gr.JSON(label="") # Functionality submit_button.click( fn=process_and_visualize, inputs=[input_text, input_model], outputs=[output_graph, output_entity_viz, output_json, stats_output] ) clear_button.click( fn=lambda: [None, None, None, ""], inputs=[], outputs=[output_graph, output_entity_viz, output_json, stats_output] ) # 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 | Created by Emergent Methods") return demo
demo = create_ui()demo.launch(share=False)app.py+158-85