Update app.py
Browse files
app.py
CHANGED
|
@@ -5,10 +5,11 @@ from datetime import datetime
|
|
| 5 |
from pixeltable.functions.huggingface import sentence_transformer
|
| 6 |
from pixeltable.functions import openai
|
| 7 |
import os
|
|
|
|
| 8 |
|
| 9 |
-
#
|
| 10 |
if 'OPENAI_API_KEY' not in os.environ:
|
| 11 |
-
os.environ['OPENAI_API_KEY'] =
|
| 12 |
|
| 13 |
# Initialize Pixeltable
|
| 14 |
pxt.drop_dir('story_builder', force=True)
|
|
@@ -65,6 +66,13 @@ story_table['continuation_response'] = openai.chat_completions(
|
|
| 65 |
max_tokens=50
|
| 66 |
)
|
| 67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
# Functions for Gradio interface
|
| 69 |
def add_contribution(contributor, content):
|
| 70 |
current_story = get_current_story()
|
|
@@ -80,7 +88,7 @@ def add_contribution(contributor, content):
|
|
| 80 |
|
| 81 |
def get_similar_parts(query, num_results=5):
|
| 82 |
sim = story_table.content.similarity(query)
|
| 83 |
-
results = story_table.order_by(sim, asc=False).limit(num_results).select(story_table.content, story_table.contributor).collect()
|
| 84 |
return results.to_pandas()
|
| 85 |
|
| 86 |
def generate_next_part():
|
|
@@ -91,6 +99,7 @@ def summarize_story():
|
|
| 91 |
summary = story_table.select(summary=story_table.summary_response.choices[0].message.content).tail(1)['summary'][0]
|
| 92 |
return summary
|
| 93 |
|
|
|
|
| 94 |
with gr.Blocks(theme=gr.themes.Base()) as demo:
|
| 95 |
gr.HTML(
|
| 96 |
"""
|
|
@@ -114,30 +123,33 @@ with gr.Blocks(theme=gr.themes.Base()) as demo:
|
|
| 114 |
with gr.TabItem("Contribute"):
|
| 115 |
with gr.Row():
|
| 116 |
with gr.Column(scale=2):
|
| 117 |
-
contributor = gr.Textbox(label="Your
|
| 118 |
content = gr.Textbox(label="Your Contribution", lines=5)
|
| 119 |
submit_btn = gr.Button("Submit Contribution", variant="primary")
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
generate_btn = gr.Button("Generate Next Part", variant="primary")
|
| 124 |
-
generated_part = gr.Textbox(label="Generated Continuation", lines=5)
|
| 125 |
-
with gr.Column():
|
| 126 |
-
summarize_btn = gr.Button("Summarize Story", variant="primary")
|
| 127 |
-
summary = gr.Textbox(label="Story Summary", lines=5)
|
| 128 |
|
| 129 |
-
with gr.TabItem("Search
|
| 130 |
with gr.Row():
|
| 131 |
with gr.Column():
|
| 132 |
search_query = gr.Textbox(label="Search Current Contributions")
|
| 133 |
num_results = gr.Slider(minimum=1, maximum=10, value=5, step=1, label="Number of Results")
|
| 134 |
search_btn = gr.Button("Search", variant="secondary")
|
| 135 |
search_results = gr.Dataframe(
|
| 136 |
-
headers=["Content", "Contributor"],
|
| 137 |
label="Similar Parts"
|
| 138 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
|
| 140 |
-
submit_btn.click(add_contribution, inputs=[contributor, content])
|
| 141 |
search_btn.click(get_similar_parts, inputs=[search_query, num_results], outputs=[search_results])
|
| 142 |
generate_btn.click(generate_next_part, outputs=[generated_part])
|
| 143 |
summarize_btn.click(summarize_story, outputs=[summary])
|
|
|
|
| 5 |
from pixeltable.functions.huggingface import sentence_transformer
|
| 6 |
from pixeltable.functions import openai
|
| 7 |
import os
|
| 8 |
+
import getpass
|
| 9 |
|
| 10 |
+
# Store API keys
|
| 11 |
if 'OPENAI_API_KEY' not in os.environ:
|
| 12 |
+
os.environ['OPENAI_API_KEY'] = getpass.getpass('OpenAI API key:')
|
| 13 |
|
| 14 |
# Initialize Pixeltable
|
| 15 |
pxt.drop_dir('story_builder', force=True)
|
|
|
|
| 66 |
max_tokens=50
|
| 67 |
)
|
| 68 |
|
| 69 |
+
# Function to get the current cumulative story
|
| 70 |
+
def get_current_story():
|
| 71 |
+
latest_entry = story_table.tail(1)
|
| 72 |
+
if len(latest_entry) > 0:
|
| 73 |
+
return latest_entry['cumulative_story'][0]
|
| 74 |
+
return ""
|
| 75 |
+
|
| 76 |
# Functions for Gradio interface
|
| 77 |
def add_contribution(contributor, content):
|
| 78 |
current_story = get_current_story()
|
|
|
|
| 88 |
|
| 89 |
def get_similar_parts(query, num_results=5):
|
| 90 |
sim = story_table.content.similarity(query)
|
| 91 |
+
results = story_table.order_by(sim, asc=False).limit(num_results).select(story_table.content, story_table.contributor, sim=sim).collect()
|
| 92 |
return results.to_pandas()
|
| 93 |
|
| 94 |
def generate_next_part():
|
|
|
|
| 99 |
summary = story_table.select(summary=story_table.summary_response.choices[0].message.content).tail(1)['summary'][0]
|
| 100 |
return summary
|
| 101 |
|
| 102 |
+
# Gradio interface
|
| 103 |
with gr.Blocks(theme=gr.themes.Base()) as demo:
|
| 104 |
gr.HTML(
|
| 105 |
"""
|
|
|
|
| 123 |
with gr.TabItem("Contribute"):
|
| 124 |
with gr.Row():
|
| 125 |
with gr.Column(scale=2):
|
| 126 |
+
contributor = gr.Textbox(label="Your Name")
|
| 127 |
content = gr.Textbox(label="Your Contribution", lines=5)
|
| 128 |
submit_btn = gr.Button("Submit Contribution", variant="primary")
|
| 129 |
+
with gr.Column(scale=3):
|
| 130 |
+
status = gr.Textbox(label="Status")
|
| 131 |
+
current_story = gr.Textbox(label="Current Story", lines=10, interactive=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
|
| 133 |
+
with gr.TabItem("Search & Generate"):
|
| 134 |
with gr.Row():
|
| 135 |
with gr.Column():
|
| 136 |
search_query = gr.Textbox(label="Search Current Contributions")
|
| 137 |
num_results = gr.Slider(minimum=1, maximum=10, value=5, step=1, label="Number of Results")
|
| 138 |
search_btn = gr.Button("Search", variant="secondary")
|
| 139 |
search_results = gr.Dataframe(
|
| 140 |
+
headers=["Content", "Contributor", "Similarity"],
|
| 141 |
label="Similar Parts"
|
| 142 |
)
|
| 143 |
+
|
| 144 |
+
with gr.Column():
|
| 145 |
+
generate_btn = gr.Button("Generate Next Part", variant="primary")
|
| 146 |
+
generated_part = gr.Textbox(label="Generated Continuation", lines=5)
|
| 147 |
+
|
| 148 |
+
with gr.TabItem("Summary"):
|
| 149 |
+
summarize_btn = gr.Button("Summarize Story", variant="primary")
|
| 150 |
+
summary = gr.Textbox(label="Story Summary", lines=8)
|
| 151 |
|
| 152 |
+
submit_btn.click(add_contribution, inputs=[contributor, content], outputs=[status, current_story])
|
| 153 |
search_btn.click(get_similar_parts, inputs=[search_query, num_results], outputs=[search_results])
|
| 154 |
generate_btn.click(generate_next_part, outputs=[generated_part])
|
| 155 |
summarize_btn.click(summarize_story, outputs=[summary])
|