Spaces:
Build error
Build error
Download util/render.py from JR818/JarvisSearchV2: direct link, hf CLI and curl.
- Browser
- Download file 3.26 kB
-
https://huggingface.co/spaces/JR818/JarvisSearchV2/resolve/main/util/render.py
- Command line
-
hf download hf://spaces/JR818/JarvisSearchV2/util/render.py
-
curl -L -o render.py https://huggingface.co/spaces/JR818/JarvisSearchV2/resolve/main/util/render.py
3.26 kB
| 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, | |
| ) |