Spaces:
Running
Running
Fix long lines and unused imports
Browse files
app.py
CHANGED
|
@@ -7,8 +7,6 @@ import os
|
|
| 7 |
import pathlib
|
| 8 |
import random
|
| 9 |
import sys
|
| 10 |
-
import tempfile
|
| 11 |
-
from typing import List, Union
|
| 12 |
|
| 13 |
import httpx
|
| 14 |
import huggingface_hub
|
|
@@ -27,8 +25,10 @@ import slidedeckai.helpers.file_manager as filem
|
|
| 27 |
from slidedeckai.helpers.chat_helper import ChatMessage, HumanMessage, AIMessage
|
| 28 |
from slidedeckai.helpers import chat_helper
|
| 29 |
|
|
|
|
| 30 |
load_dotenv()
|
| 31 |
|
|
|
|
| 32 |
class StreamlitChatMessageHistory:
|
| 33 |
"""Chat message history stored in Streamlit session state."""
|
| 34 |
|
|
@@ -39,14 +39,18 @@ class StreamlitChatMessageHistory:
|
|
| 39 |
|
| 40 |
@property
|
| 41 |
def messages(self):
|
|
|
|
| 42 |
return st.session_state[self.key]
|
| 43 |
|
| 44 |
def add_user_message(self, content: str):
|
|
|
|
| 45 |
st.session_state[self.key].append(HumanMessage(content))
|
| 46 |
|
| 47 |
def add_ai_message(self, content: str):
|
|
|
|
| 48 |
st.session_state[self.key].append(AIMessage(content))
|
| 49 |
|
|
|
|
| 50 |
RUN_IN_OFFLINE_MODE = os.getenv('RUN_IN_OFFLINE_MODE', 'False').lower() == 'true'
|
| 51 |
|
| 52 |
|
|
@@ -56,7 +60,6 @@ def _load_strings() -> dict:
|
|
| 56 |
Load various strings to be displayed in the app.
|
| 57 |
:return: The dictionary of strings.
|
| 58 |
"""
|
| 59 |
-
|
| 60 |
with open(GlobalConfig.APP_STRINGS_FILE, 'r', encoding='utf-8') as in_file:
|
| 61 |
return json5.loads(in_file.read())
|
| 62 |
|
|
@@ -233,8 +236,14 @@ with st.sidebar:
|
|
| 233 |
selected_provider = provider_match.group(1)
|
| 234 |
else:
|
| 235 |
# If regex doesn't match, try to extract provider from the beginning
|
| 236 |
-
selected_provider =
|
| 237 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 238 |
|
| 239 |
# Validate that the selected provider is valid
|
| 240 |
if selected_provider not in GlobalConfig.VALID_PROVIDERS:
|
|
@@ -400,11 +409,16 @@ def set_up_chat_ui():
|
|
| 400 |
progress_bar = st.progress(0, 'Preparing to call LLM...')
|
| 401 |
|
| 402 |
def progress_callback(current_progress):
|
| 403 |
-
progress_bar.progress(
|
|
|
|
|
|
|
|
|
|
| 404 |
|
| 405 |
try:
|
| 406 |
if _is_it_refinement():
|
| 407 |
-
path = slide_generator.revise(
|
|
|
|
|
|
|
| 408 |
else:
|
| 409 |
path = slide_generator.generate(progress_callback=progress_callback)
|
| 410 |
|
|
@@ -484,43 +498,24 @@ def _is_it_refinement() -> bool:
|
|
| 484 |
return False
|
| 485 |
|
| 486 |
|
| 487 |
-
def _get_user_messages() ->
|
| 488 |
"""
|
| 489 |
Get a list of user messages submitted until now from the session state.
|
| 490 |
|
| 491 |
:return: The list of user messages.
|
| 492 |
"""
|
| 493 |
-
|
| 494 |
return [
|
| 495 |
-
msg.content for msg in st.session_state[CHAT_MESSAGES]
|
|
|
|
| 496 |
]
|
| 497 |
|
| 498 |
|
| 499 |
-
def _get_last_response() -> str:
|
| 500 |
-
"""
|
| 501 |
-
Get the last response generated by AI.
|
| 502 |
-
|
| 503 |
-
:return: The response text.
|
| 504 |
-
"""
|
| 505 |
-
|
| 506 |
-
def _display_messages_history(view_messages: st.expander):
|
| 507 |
-
"""
|
| 508 |
-
Display the history of messages.
|
| 509 |
-
|
| 510 |
-
:param view_messages: The list of AI and Human messages.
|
| 511 |
-
"""
|
| 512 |
-
|
| 513 |
-
with view_messages:
|
| 514 |
-
view_messages.json(st.session_state[CHAT_MESSAGES])
|
| 515 |
-
|
| 516 |
-
|
| 517 |
def _display_download_button(file_path: pathlib.Path):
|
| 518 |
"""
|
| 519 |
Display a download button to download a slide deck.
|
| 520 |
|
| 521 |
:param file_path: The path of the .pptx file.
|
| 522 |
"""
|
| 523 |
-
|
| 524 |
with open(file_path, 'rb') as download_file:
|
| 525 |
st.download_button(
|
| 526 |
'Download PPTX file ⬇️',
|
|
|
|
| 7 |
import pathlib
|
| 8 |
import random
|
| 9 |
import sys
|
|
|
|
|
|
|
| 10 |
|
| 11 |
import httpx
|
| 12 |
import huggingface_hub
|
|
|
|
| 25 |
from slidedeckai.helpers.chat_helper import ChatMessage, HumanMessage, AIMessage
|
| 26 |
from slidedeckai.helpers import chat_helper
|
| 27 |
|
| 28 |
+
|
| 29 |
load_dotenv()
|
| 30 |
|
| 31 |
+
|
| 32 |
class StreamlitChatMessageHistory:
|
| 33 |
"""Chat message history stored in Streamlit session state."""
|
| 34 |
|
|
|
|
| 39 |
|
| 40 |
@property
|
| 41 |
def messages(self):
|
| 42 |
+
"""Get all chat messages in the history."""
|
| 43 |
return st.session_state[self.key]
|
| 44 |
|
| 45 |
def add_user_message(self, content: str):
|
| 46 |
+
"""Add a user message to the history."""
|
| 47 |
st.session_state[self.key].append(HumanMessage(content))
|
| 48 |
|
| 49 |
def add_ai_message(self, content: str):
|
| 50 |
+
"""Add an AI message to the history."""
|
| 51 |
st.session_state[self.key].append(AIMessage(content))
|
| 52 |
|
| 53 |
+
|
| 54 |
RUN_IN_OFFLINE_MODE = os.getenv('RUN_IN_OFFLINE_MODE', 'False').lower() == 'true'
|
| 55 |
|
| 56 |
|
|
|
|
| 60 |
Load various strings to be displayed in the app.
|
| 61 |
:return: The dictionary of strings.
|
| 62 |
"""
|
|
|
|
| 63 |
with open(GlobalConfig.APP_STRINGS_FILE, 'r', encoding='utf-8') as in_file:
|
| 64 |
return json5.loads(in_file.read())
|
| 65 |
|
|
|
|
| 236 |
selected_provider = provider_match.group(1)
|
| 237 |
else:
|
| 238 |
# If regex doesn't match, try to extract provider from the beginning
|
| 239 |
+
selected_provider = (
|
| 240 |
+
llm_provider_to_use.split(' ')[0]
|
| 241 |
+
if ' ' in llm_provider_to_use else llm_provider_to_use
|
| 242 |
+
)
|
| 243 |
+
logger.warning(
|
| 244 |
+
'Provider regex did not match for: %s, using: %s',
|
| 245 |
+
llm_provider_to_use, selected_provider
|
| 246 |
+
)
|
| 247 |
|
| 248 |
# Validate that the selected provider is valid
|
| 249 |
if selected_provider not in GlobalConfig.VALID_PROVIDERS:
|
|
|
|
| 409 |
progress_bar = st.progress(0, 'Preparing to call LLM...')
|
| 410 |
|
| 411 |
def progress_callback(current_progress):
|
| 412 |
+
progress_bar.progress(
|
| 413 |
+
min(current_progress / gcfg.get_max_output_tokens(llm_provider_to_use), 0.95),
|
| 414 |
+
text='Streaming content...this might take a while...'
|
| 415 |
+
)
|
| 416 |
|
| 417 |
try:
|
| 418 |
if _is_it_refinement():
|
| 419 |
+
path = slide_generator.revise(
|
| 420 |
+
instructions=prompt_text, progress_callback=progress_callback
|
| 421 |
+
)
|
| 422 |
else:
|
| 423 |
path = slide_generator.generate(progress_callback=progress_callback)
|
| 424 |
|
|
|
|
| 498 |
return False
|
| 499 |
|
| 500 |
|
| 501 |
+
def _get_user_messages() -> list[str]:
|
| 502 |
"""
|
| 503 |
Get a list of user messages submitted until now from the session state.
|
| 504 |
|
| 505 |
:return: The list of user messages.
|
| 506 |
"""
|
|
|
|
| 507 |
return [
|
| 508 |
+
msg.content for msg in st.session_state[CHAT_MESSAGES]
|
| 509 |
+
if isinstance(msg, chat_helper.HumanMessage)
|
| 510 |
]
|
| 511 |
|
| 512 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 513 |
def _display_download_button(file_path: pathlib.Path):
|
| 514 |
"""
|
| 515 |
Display a download button to download a slide deck.
|
| 516 |
|
| 517 |
:param file_path: The path of the .pptx file.
|
| 518 |
"""
|
|
|
|
| 519 |
with open(file_path, 'rb') as download_file:
|
| 520 |
st.download_button(
|
| 521 |
'Download PPTX file ⬇️',
|