File size: 1,501 Bytes
bb76ad8
 
 
 
 
08567a8
bb76ad8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4d2e485
bb76ad8
 
 
 
 
 
 
08567a8
 
bb76ad8
fd9b3c0
bb76ad8
 
fd9b3c0
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import gradio as gr
import rdflib

# Load the Turtle file and initialize the graph
g = rdflib.Graph()
g.parse('huggingface-30k.ttl')

# Define the function to execute the SPARQL query
def run_sparql(query):
    try:
        qres = g.query(query)
        results = []
        for row in qres:
            # Concatenate each row's results into a readable string
            result = " | ".join(str(cell) for cell in row)
            results.append(result)
        return "\n".join(results) if results else "No results found."
    except Exception as e:
        return f"Error: {e}"

# Gradio interface
query_input = gr.Textbox(label="SPARQL Query", lines=5, placeholder="Enter SPARQL query here", value="SELECT DISTINCT ?b WHERE {?a ?b ?c}")
output_text = gr.Textbox(label="Query Results", lines=10)

# Launch the app
demo = gr.Interface(
    fn=run_sparql,
    inputs=query_input,
    outputs=output_text,
    title="Hugging Face Datasets SPARQL Query Interface (medium subset)",
    description="Enter a SPARQL query to retrieve data from 30k Hugging Face datasets. Generated by [huggingface-rdf](https://github.com/david4096/huggingface-rdf). [Download the turtle file locally](https://huggingface.co./datasets/david4096/huggingface-ttl/resolve/main/huggingface-30k.ttl?download=true) and load it into your [favorite triple store](https://github.com/david4096/huggingface-rdf/tree/main/qlever_scripts)!",
)

if __name__ == "__main__":
    demo.launch()



if __name__ == "__main__":
    demo.launch()