Spaces:
Build error

File size: 3,260 Bytes
e04ef02
250bc3c
595b503
 
250bc3c
 
595b503
 
 
 
250bc3c
 
e04ef02
8aa08e9
595b503
 
250bc3c
 
 
 
 
8aa08e9
 
595b503
 
 
 
250bc3c
 
 
 
 
 
595b503
250bc3c
 
 
595b503
037bed1
250bc3c
 
 
55fa4e3
 
250bc3c
 
 
 
 
 
 
 
 
 
 
 
595b503
250bc3c
 
 
 
 
 
 
 
 
595b503
 
 
250bc3c
595b503
e04ef02
 
250bc3c
 
 
 
 
 
 
037bed1
250bc3c
 
 
 
 
 
55fa4e3
 
250bc3c
 
 
 
 
 
 
 
 
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
import re
from typing import Any
import streamlit as st
import types
from streamlit_card import card



class Render():
    
    SOURCE_CARD={
        "card": {"width": "100%","height":"60px","margin":"0px","borderRadius": "3px","box-shadow":"0px 0px 0px rgba(0, 0, 0, 0)"},
        "title":{"font-size":"0.9em","text-align":"left","width":"90%"},
        "text":{"text-align":"left","font-size":"0.6em","text-align":"left","width":"90%"},
        "filter": {"background-color": "rgba(50, 50, 50, 0.5)"}
    }
    SUGGESTION_CARD={
        "card": {"width": "100%","height":"60px","margin":"0px","borderRadius": "3px","box-shadow":"0px 0px 0px rgba(0, 0, 0, 0)"},
        "title":{"font-size":"0.9em","text-align":"left","width":"90%"},
        "filter": {"background-color": "rgba(50, 50, 50, 0.5)"}
    }

    CARD_NUM = 3
    
    def __init__(self) -> None:
        pass

    def __call__(self, answer=None):
        print(st.session_state)
        self.render_suggestion()
        self.render_source()
        self.render_answer(answer=answer)
        

    def render_suggestion(self):
        suggestion = st.session_state.suggestion
        if not suggestion:
            return
        cols = st.columns([2,2,2,2,2,2],gap="small")
        for idx,item in enumerate(suggestion):
            name = item.get('name','')
            url = item.get('url','')
            if name == "" or url == "":
                continue
            with cols[idx]:
                try:
                    print(f"suggestion_{idx}")
                    card(
                        key=f"suggestion_{idx}",
                        title=name,
                        text='',
                        styles=Render.SUGGESTION_CARD,
                        url=url,
                    )   
                except Exception as e:
                    pass

    def render_answer(self,answer):
        if st.session_state.answer:
            st.subheader("Answer")
            text_container = st.empty()
            text_container.empty()
            text_container.write(st.session_state.answer)
            return
        if not answer:
            return
        st.subheader("Answer")
        text_container = st.empty()
        text = ""
        for data in answer:
            text += data
            text_container.empty()
            text_container.write(text)
        st.session_state.answer = text

    def render_source(self):
        source = st.session_state.source
        if not source:
            return
        st.subheader("Source")
        cols = st.columns([2,2,2,2],gap="small")
        for idx,item in enumerate(source):
            title = item.get("name","")
            title = re.search(r"^(.{0,60}\S\b|)", title).group(0)
            text = item.get("description","")
            text = re.search(r"^(.{0,120}\S\b|)", text).group(0)+" ..."
            url = item.get("url","")
            if title == "" or text == "" or url == "":
                continue
            with cols[idx]:
                print(f"source_{idx}")
                card(
                    key=f"source_{idx}",
                    title=title,
                    text='',
                    styles=Render.SOURCE_CARD,
                    url=url,
                )