File size: 4,907 Bytes
6b0801d
53eeb1f
78d5505
 
358d368
6b0801d
79e0d98
53eeb1f
358d368
 
 
78d5505
 
53eeb1f
a763d7f
 
 
78d5505
 
 
816a1fb
78d5505
5ed091b
78d5505
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a24f06e
78d5505
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a24f06e
 
78d5505
 
cb89fae
78d5505
a24f06e
78d5505
 
53eeb1f
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import os
import gradio as gr
import pandas as pd
from groq import Groq
from dotenv import load_dotenv

os.environ['REQUESTS_CA_BUNDLE'] = '/usr/local/share/zscaler.crt'


load_dotenv()

def excel_to_csv(fi):
    df = pd.read_excel(fi)

    clm = []
    for c in df.columns:
        clm.append(c)
    
    df = df.to_csv(path_or_buf = "here2.csv")

    return gr.Dropdown(choices=clm, value=clm, label="Columns of the file", multiselect=True, allow_custom_value=True), "here2.csv"

def text_to_neo4j(rm, cm):
    prompt = """
    
    Here is a random Neo4J database with additionnal informations in '#' :  
    
    (d:Document {URL: string (#contains the url of the document), type: string (#contains the type of the document), Description: string (#contains the description of the document)} -[:TALKS_ABOUT]-> (t: Topic {title: string (#contains the title of the topic), description: string (#contains the description of the topic)}) <-[:INTERESTED_BY]- (e: Expert {name: string (#contains the name of the expert)};
    
      
    (s:Solution {url: string (#contains the url of the document the solution refers to), description: string (#contains the description of the solution})-[:SOLUTION_OF]->(d)
    (p:Problem {url: string (#contains the url of the document the problem refers to), description: string (#contains the description of the problem})-[:PROBLEM_OF]->(d);
    
    ---
    Here is a exemple of a Cypher script for this data batase: 
    
    //KeyIssues
    LOAD CSV WITH HEADERS FROM 'file:///dataKeyIssues.csv' AS row
    FIELDTERMINATOR ';'
    WITH row WHERE row.Expert IS NOT NULL AND row.Problems IS NULL  
    WITH row, split(replace(row.Description,", This topic","This topic"), 'This topic') as Descriptions, split(row.Topic, ',') as Topics, split(row.Expert, ',') AS Experts, split(row.Score, ',') as Score 
    
    MERGE (dd:Document {uri: row.Document, description: row.`Key Issue`, type: "TR"})
    
    FOREACH (i IN RANGE(0, size(Descriptions) - 2) |
        MERGE (t:Topic {name: trim(Topics[i]), description: trim(Descriptions[i+1])})
        MERGE (dd)-[r:TALKS_ABOUT{}]->(t)
             ON CREATE set r.score = Score[i]
             ON MATCH SET
             r.score = CASE WHEN Score[i] > r.score 
             THEN Score[i]
             ELSE r.score END
        MERGE (e:Expert {name: trim(Experts[i])})
        MERGE (e)-[:INTERESTED_BY]->(t)
    );
    
    LOAD CSV WITH HEADERS FROM 'file:///dataKeyIssues.csv' AS row
    FIELDTERMINATOR ';'
    WITH row WHERE  row.Problems IS NOT NULL  
    WITH row, split(replace(row.Description,", This topic","This topic"), 'This topic') as Descriptions, split(row.Topic, ',') as Topics
    
    MERGE (dd:Document {uri: row.Document, keyIssues: row.`Key Issue`, type: "TR"})
    MERGE (p:Problem {description: row.Problems})
    MERGE (p)-[:PROBLEM_OF]->(dd)
    ---
    With the first line of a CSV file, provide me a Neo4J Cypher script which implement the csv file in the database. Here are some remarks about this file : %s .Only provide a script based on the data given in the CSV file: 
    
    First line = %s
    """ % (rm,cm)

    messages = [
        {
            "role": "system",
            "content": f"You are a helpful assistant. Only show your final response to the **User Query**! Do not provide any explanations or details."
        },
        {
            "role": "user",
            "content": prompt,
        }
    ]
    
    client = Groq(api_key= os.environ["GROQ_API_KEY1"])
    chat_completion = client.chat.completions.create(
        messages=messages,
        model="llama3-70b-8192",
    )
    
    response = chat_completion.choices[0].message.content
    print(response)
    
    text_file = open("Output.txt", "w")
    
    text_file.write(response)
    
    text_file.close()
    
    return "Output.txt",response



with gr.Blocks() as demo:
    
    with gr.Tab("Excel to Neo4J"):
        gr.Markdown("### Transfer your excel data in a Neo4J database using this tool !")

        ex_fi = gr.File(file_count='single')
        csv_fi = gr.File(file_count='single')

        columns = gr.Dropdown(label="Columns of the file", multiselect=True, allow_custom_value=True)

        remarks = gr.Dropdown(["This file do not contain information about Topic or Expert Nodes", "This file do not contain information about Document Nodes", "This file do not contain information about Solution Nodes", "This file do not contain information about Problem nodes"], label="Remarks", multiselect=True, allow_custom_value=True)

        btn_submit = gr.Button("Submit")

        result_ta = gr.TextArea("Here you will find your answer !")

        result_fi = gr.File(file_count='single')
        
        ex_fi.upload(excel_to_csv, inputs=ex_fi, outputs=[columns, csv_fi])

        btn_submit.click(text_to_neo4j, inputs=[remarks, columns], outputs=[result_fi,result_ta])
        
    
demo.launch()