barunsaha commited on
Commit
da03db8
1 Parent(s): 9c0dccd

Add chat interface and generate JSON content; update other elements

Browse files
Files changed (4) hide show
  1. chat_app.py +123 -0
  2. global_config.py +10 -1
  3. requirements.txt +4 -1
  4. strings.json +10 -3
chat_app.py ADDED
@@ -0,0 +1,123 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import logging
2
+ import random
3
+
4
+ import json5
5
+ import streamlit as st
6
+ from langchain_community.chat_message_histories import (
7
+ StreamlitChatMessageHistory
8
+ )
9
+ from langchain_core.prompts import ChatPromptTemplate
10
+ from langchain_core.runnables.history import RunnableWithMessageHistory
11
+
12
+ from global_config import GlobalConfig
13
+ from helpers import llm_helper
14
+
15
+
16
+ APP_TEXT = json5.loads(open(GlobalConfig.APP_STRINGS_FILE, 'r', encoding='utf-8').read())
17
+ # langchain.debug = True
18
+ # langchain.verbose = True
19
+
20
+ logger = logging.getLogger(__name__)
21
+ progress_bar = st.progress(0, text='Setting up SlideDeck AI...')
22
+
23
+
24
+ def display_page_header_content():
25
+ """
26
+ Display content in the page header.
27
+ """
28
+
29
+ st.title(APP_TEXT['app_name'])
30
+ st.subheader(APP_TEXT['caption'])
31
+ st.markdown(
32
+ 'Powered by'
33
+ ' [Mistral-7B-Instruct-v0.2](https://huggingface.co/mistralai/Mistral-7B-Instruct-v0.2)'
34
+ )
35
+
36
+
37
+ def display_page_footer_content():
38
+ """
39
+ Display content in the page footer.
40
+ """
41
+
42
+ st.text(APP_TEXT['tos'] + '\n' + APP_TEXT['tos2'])
43
+ # st.markdown(
44
+ # '![Visitors](https://api.visitorbadge.io/api/visitors?path=https%3A%2F%2Fhuggingface.co%2Fspaces%2Fbarunsaha%2Fslide-deck-ai&countColor=%23263759)' # noqa: E501
45
+ # )
46
+
47
+
48
+ def build_ui():
49
+ """
50
+ Display the input elements for content generation.
51
+ """
52
+
53
+ display_page_header_content()
54
+
55
+ with st.expander('Usage Policies and Limitations'):
56
+ display_page_footer_content()
57
+
58
+ progress_bar.progress(50, text='Setting up chat interface...')
59
+ set_up_chat_ui()
60
+
61
+
62
+ def set_up_chat_ui():
63
+ """
64
+ Prepare the chat interface and related functionality.
65
+ """
66
+
67
+ history = StreamlitChatMessageHistory(key='chat_messages')
68
+ llm = llm_helper.get_hf_endpoint()
69
+
70
+ with open(
71
+ 'langchain_templates/template_combined_chat_history.txt',
72
+ 'r',
73
+ encoding='utf-8'
74
+ ) as in_file:
75
+ template = in_file.read()
76
+
77
+ prompt = ChatPromptTemplate.from_template(template)
78
+
79
+ chain = prompt | llm
80
+
81
+ chain_with_history = RunnableWithMessageHistory(
82
+ chain,
83
+ lambda session_id: history, # Always return the instance created earlier
84
+ input_messages_key='question',
85
+ history_messages_key='chat_history',
86
+ )
87
+
88
+ with st.expander('Usage Instructions'):
89
+ st.write(GlobalConfig.CHAT_USAGE_INSTRUCTIONS)
90
+
91
+ st.chat_message('ai').write(
92
+ random.choice(APP_TEXT['ai_greetings'])
93
+ )
94
+
95
+ # for msg in history.messages:
96
+ # st.chat_message(msg.type).write(msg.content)
97
+
98
+ progress_bar.progress(100, text='Done!')
99
+ progress_bar.empty()
100
+
101
+ if prompt := st.chat_input(
102
+ placeholder=APP_TEXT['chat_placeholder'],
103
+ max_chars=GlobalConfig.LLM_MODEL_MAX_INPUT_LENGTH
104
+ ):
105
+ logger.debug('User input: %s', prompt)
106
+ st.chat_message('user').write(prompt)
107
+
108
+ # As usual, new messages are added to StreamlitChatMessageHistory when the Chain is called
109
+ config = {'configurable': {'session_id': 'any'}}
110
+ response = chain_with_history.invoke({'question': prompt}, config)
111
+ st.chat_message('ai').markdown('```json\n' + response)
112
+
113
+
114
+ def main():
115
+ """
116
+ Trigger application run.
117
+ """
118
+
119
+ build_ui()
120
+
121
+
122
+ if __name__ == '__main__':
123
+ main()
global_config.py CHANGED
@@ -14,7 +14,7 @@ class GlobalConfig:
14
  LLM_MODEL_TEMPERATURE: float = 0.2
15
  LLM_MODEL_MIN_OUTPUT_LENGTH: int = 50
16
  LLM_MODEL_MAX_OUTPUT_LENGTH: int = 2000
17
- LLM_MODEL_MAX_INPUT_LENGTH: int = 300
18
 
19
  HUGGINGFACEHUB_API_TOKEN = os.environ.get('HUGGINGFACEHUB_API_TOKEN', '')
20
  METAPHOR_API_KEY = os.environ.get('METAPHOR_API_KEY', '')
@@ -40,6 +40,15 @@ class GlobalConfig:
40
  }
41
  }
42
 
 
 
 
 
 
 
 
 
 
43
 
44
  logging.basicConfig(
45
  level=GlobalConfig.LOG_LEVEL,
 
14
  LLM_MODEL_TEMPERATURE: float = 0.2
15
  LLM_MODEL_MIN_OUTPUT_LENGTH: int = 50
16
  LLM_MODEL_MAX_OUTPUT_LENGTH: int = 2000
17
+ LLM_MODEL_MAX_INPUT_LENGTH: int = 100
18
 
19
  HUGGINGFACEHUB_API_TOKEN = os.environ.get('HUGGINGFACEHUB_API_TOKEN', '')
20
  METAPHOR_API_KEY = os.environ.get('METAPHOR_API_KEY', '')
 
40
  }
41
  }
42
 
43
+ CHAT_USAGE_INSTRUCTIONS = (
44
+ 'Briefly describe your topic of presentation in the textbox provided below.'
45
+ ' Subsequently, you can add follow-up instructions, e.g., "Can you add a slide on GPUs?"'
46
+ ' You can also ask AI to refine any particular slide, e.g., "Make the slide with title'
47
+ ' \'Examples of AI\' a bit more descriptive."'
48
+ '\n\n'
49
+ 'SlideDeck AI generates only text content. It does not have access to the Internet.'
50
+ )
51
+
52
 
53
  logging.basicConfig(
54
  level=GlobalConfig.LOG_LEVEL,
requirements.txt CHANGED
@@ -6,4 +6,7 @@ streamlit~=1.32.2
6
  python-pptx
7
  metaphor-python
8
  json5~=0.9.14
9
- requests~=2.31.0
 
 
 
 
6
  python-pptx
7
  metaphor-python
8
  json5~=0.9.14
9
+ requests~=2.31.0
10
+
11
+ transformers
12
+ langchain-community
strings.json CHANGED
@@ -1,6 +1,6 @@
1
  {
2
- "app_name": "SlideDeck AI",
3
- "caption": "*:green[Co-create your next PowerPoint slide deck with AI]*",
4
  "section_headers": [
5
  "Step 1: Generate your content",
6
  "Step 2: Make it structured",
@@ -25,5 +25,12 @@
25
  "image_info": "Got some more minutes? We are also trying to deliver an AI-generated art on the presentation topic, fresh off the studio, just for you!",
26
  "content_generation_failure_error": "Unfortunately, SlideDeck AI failed to generate any content for you! Please try again later.",
27
  "tos": "SlideDeck AI is an experimental prototype, and it has its limitations.\nPlease carefully review any and all AI-generated content.",
28
- "tos2": "By using SlideDeck AI, you agree to fair and responsible usage.\nNo liability assumed by any party."
 
 
 
 
 
 
 
29
  }
 
1
  {
2
+ "app_name": ":green[SlideDeck AI [Reloaded]]",
3
+ "caption": "Converse, create, iterate, and improve your PowerPoint slide deck",
4
  "section_headers": [
5
  "Step 1: Generate your content",
6
  "Step 2: Make it structured",
 
25
  "image_info": "Got some more minutes? We are also trying to deliver an AI-generated art on the presentation topic, fresh off the studio, just for you!",
26
  "content_generation_failure_error": "Unfortunately, SlideDeck AI failed to generate any content for you! Please try again later.",
27
  "tos": "SlideDeck AI is an experimental prototype, and it has its limitations.\nPlease carefully review any and all AI-generated content.",
28
+ "tos2": "By using SlideDeck AI, you agree to fair and responsible usage.\nNo liability assumed by any party.",
29
+ "ai_greetings": [
30
+ "How may I help you today?",
31
+ "Stuck with creating your presentation? Let me help you.",
32
+ "Looks like you have a looming deadline. Can I help you start with your slide deck?",
33
+ "Hello! What topic do you have on your mind today?"
34
+ ],
35
+ "chat_placeholder": "Write the topic or instructions here"
36
  }