File size: 1,754 Bytes
0827183
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from conn import connector
import time
import csv
import pandas as pd
import os,re,zipfile


conn=connector()
info=conn.generate()
info=conn.start_conversation(info['token'])

def resp(message, intensity):

    """Direct Line Api v.3 connection to CoPilot"""
    retval = conn.send_message(info['token'],info['conversationId'],'cohitai',message)
    time.sleep(4)
    return conn.get_message(info['token'],info['conversationId'])["activities"][-1]["text"]

def upload_file(files):
    return [pd.read_excel(file.name) for file in files]
    

def process_excel_files(files):
    temp_list=[(file,fetch_sheet_names(file)) for file in files]
    df = [pd.read_excel(tup[0],sheet) for tup in temp_list for sheet in tup[1]]
    return df

def fetch_sheet_names(path):
    sheets=[]
    with zipfile.ZipFile(path,'r') as zip_ref:
        xml=zip_ref.read("xl/workbook.xml").decode("utf-8")
        for s_tag in re.findall("<sheet [^>]*",xml) : sheets.append(re.search('name="[^"]*',s_tag).group(0)[6:])

    return sheets

with gr.Blocks() as demo:
    gr.Markdown("# Say Hello to Validify-Bot, Built on Microsoft Co-Pilot Backend, with Direct Line API V. 3.0")
    inp = gr.Textbox(placeholder="Please Enter the text here.")
    out = gr.Textbox()
    inp.change(resp, inp, out)

    file_output = gr.Files()
    upload_button = gr.UploadButton("Click to Upload Excel Files", file_types=["file"], file_count="multiple")
    dataset = gr.Dataframe(row_count=5)
    upload_button.upload(fn=process_excel_files, inputs=upload_button, outputs=dataset)
    

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



# demo = gr.Interface(
#     fn=resp,
#     # fn=file_input,
#     inputs=["text","slider"],
#     outputs=["text"],
# )