Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ohyeon5/path #17

Merged
merged 3 commits into from
Nov 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
openai==0.25.0
14 changes: 7 additions & 7 deletions ui/app.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import openai
import streamlit as st
import itertools
from typing import Optional, Callable, Union

from rip.utils import initialize_openai_api, DEFAULT_IMG_URL
Expand Down Expand Up @@ -53,12 +54,13 @@ def suggest_actions(question:str, keywords: str):
return actions

def suggest_urls(keywords: str):
keywords = keywords.split(sep=',')
urls = search_relevant_urls(keywords)
keywords = list(itertools.chain(*[k.split(sep=' ') for k in keywords.split(sep=',')]))
title_url_dict = search_relevant_urls(keywords)
st.subheader(
"Here are some interesting resources to have a look :)"
"Here are some related policies to have a look :)"
)
st.write(urls)
text = "\n\n ".join([f"[{title}]({url})" for title, url in title_url_dict.items()])
st.write(text)


def tldr():
Expand Down Expand Up @@ -118,6 +120,4 @@ def dalle2(
with st.form(key="tldr_res"):
tldr_text = tldr()
dalle2(tldr_text, transform_text_to_scene_description)
submit2 = st.form_submit_button("DALLE")


submit2 = st.form_submit_button("DALLE")
51 changes: 48 additions & 3 deletions ui/components/explore_data.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,49 @@
from typing import List
import os
import json
import logging
import requests
import io
from urllib.parse import quote
from typing import List, Dict
logging.basicConfig(level=os.environ.get("LOGLEVEL", "INFO"))

def search_relevant_urls(keywords: List[str])-> List[str]:
return keywords
DATA_PATH = "C:\\Users\\ohyeo\\OneDrive\\rip\\open-data-main\\data\\USA\\"
DATA_URL = "https://raw.githubusercontent.com/climatepolicyradar/open-data/main/data/USA/"


def argsort(seq):
return sorted(range(len(seq)), key=seq.__getitem__)

def val_in_list(val, list_val):
return any([val in v for v in list_val])

def get_json_from_path(data_path=DATA_PATH):
data = []
for fn in os.listdir(data_path):
if '.json' in fn:
fname = os.path.join(data_path, fn)
with open(fname, 'r', encoding="utf8") as f:
data.append(json.load(f))
logging.info(f"There are {len(data)} json files in {data_path}")
return data

def get_json_from_url(data_url = DATA_URL):
download = requests.get(data_url).content
print(download)


def search_relevant_urls(keywords: List[str], n: int=3)-> Dict[str, str]:
data = get_json_from_path(DATA_PATH)
dump_all_idx = []
for k in keywords:
dump_all_idx.extend([idx for idx, d in enumerate(data) if val_in_list(k.strip().lower(), list(map(str.lower, d["document_keyword"])))])
unique = list(set(dump_all_idx))
counts = [dump_all_idx.count(idx) for idx in unique]
unique = [unique[idx] for idx in argsort(counts)]
fin_idx = unique[:min(n, len(unique))]
data = [data[idx] for idx in fin_idx]

return {title: url.replace(" ", "%20") for title, url in [[d["document_name"], d["document_url"]] for d in data]}

if __name__=="__main__":
get_json_from_url()