Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,7 +4,31 @@ from knowledge_storm.lm import OpenAIModel
|
|
| 4 |
from knowledge_storm.rm import YouRM
|
| 5 |
import spaces
|
| 6 |
import gradio as gr
|
|
|
|
|
|
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
lm_configs = STORMWikiLMConfigs()
|
| 9 |
openai_kwargs = {
|
| 10 |
'api_key': os.getenv("OPENAI_API_KEY"),
|
|
@@ -42,7 +66,10 @@ def generate_article(prompt, progress=gr.Progress(track_tqdm=True)):
|
|
| 42 |
generated_folder = prompt.replace(" ", "_")
|
| 43 |
with open(f'outputs/{generated_folder}/storm_gen_article.txt', 'r') as file:
|
| 44 |
content = file.read()
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
with gr.Blocks() as demo:
|
| 48 |
gr.Markdown("# Omnipedia article generation demo (Storm GPT-4 + You)")
|
|
|
|
| 4 |
from knowledge_storm.rm import YouRM
|
| 5 |
import spaces
|
| 6 |
import gradio as gr
|
| 7 |
+
import json
|
| 8 |
+
import re
|
| 9 |
|
| 10 |
+
def convert_references_to_links(text, json_data):
|
| 11 |
+
url_mapping = json_data['url_to_unified_index']
|
| 12 |
+
|
| 13 |
+
# Function to replace references with markdown links
|
| 14 |
+
def replace_reference(match):
|
| 15 |
+
ref_num = match.group(1)
|
| 16 |
+
url = next((url for url, index in url_mapping.items() if str(index) == ref_num), None)
|
| 17 |
+
if url:
|
| 18 |
+
return f'[{match.group(0)}]({url})'
|
| 19 |
+
return match.group(0)
|
| 20 |
+
|
| 21 |
+
# Replace references in the text
|
| 22 |
+
processed_text = re.sub(r'\[(\d+)\]', replace_reference, text)
|
| 23 |
+
|
| 24 |
+
# Generate reference list
|
| 25 |
+
reference_list = [f"[{index}] {url}" for url, index in sorted(url_mapping.items(), key=lambda x: x[1])]
|
| 26 |
+
|
| 27 |
+
# Combine processed text and reference list
|
| 28 |
+
markdown_output = f"{processed_text}\n\n" + "\n".join(reference_list)
|
| 29 |
+
|
| 30 |
+
return markdown_output
|
| 31 |
+
|
| 32 |
lm_configs = STORMWikiLMConfigs()
|
| 33 |
openai_kwargs = {
|
| 34 |
'api_key': os.getenv("OPENAI_API_KEY"),
|
|
|
|
| 66 |
generated_folder = prompt.replace(" ", "_")
|
| 67 |
with open(f'outputs/{generated_folder}/storm_gen_article.txt', 'r') as file:
|
| 68 |
content = file.read()
|
| 69 |
+
with open(f'outputs/{generated_folder}/url_to_info.json', 'r') as file:
|
| 70 |
+
references_json = json.load(file)
|
| 71 |
+
article_full = convert_references_to_links(f'# {prompt}\n\n'+content, references_json)
|
| 72 |
+
return article_full
|
| 73 |
|
| 74 |
with gr.Blocks() as demo:
|
| 75 |
gr.Markdown("# Omnipedia article generation demo (Storm GPT-4 + You)")
|