ofermend commited on
Commit
d5ed529
β€’
1 Parent(s): 3b98ec7
Files changed (4) hide show
  1. README.md +5 -4
  2. Vectara-logo.png +0 -0
  3. app.py +135 -0
  4. requirements.txt +6 -0
README.md CHANGED
@@ -1,13 +1,14 @@
1
  ---
2
  title: Justice Harvard
3
- emoji: πŸ”₯
4
- colorFrom: red
5
- colorTo: red
6
  sdk: streamlit
7
- sdk_version: 1.34.0
8
  app_file: app.py
9
  pinned: false
10
  license: apache-2.0
 
11
  ---
12
 
13
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
  title: Justice Harvard
3
+ emoji: 🐨
4
+ colorFrom: indigo
5
+ colorTo: indigo
6
  sdk: streamlit
7
+ sdk_version: 1.32.2
8
  app_file: app.py
9
  pinned: false
10
  license: apache-2.0
11
+ short_description: Teacher Assistant - Justice Harvard
12
  ---
13
 
14
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
Vectara-logo.png ADDED
app.py ADDED
@@ -0,0 +1,135 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ from omegaconf import OmegaConf
3
+ from query import VectaraQuery
4
+ import streamlit as st
5
+ import os
6
+ from PIL import Image
7
+ import re
8
+ from translate import Translator
9
+
10
+
11
+ from llama_index.indices.managed.vectara import VectaraIndex
12
+ from llama_index.core.agent import ReActAgent
13
+ from llama_index.llms.openai import OpenAI
14
+ from llama_index.core.tools import QueryEngineTool, ToolMetadata
15
+ from llama_index.core.utils import print_text
16
+
17
+ learning_styles = ['traditional', 'inquiry based']
18
+ languages = {'English': 'en', 'Spanish': 'es', 'French': 'fr', 'German': 'de', 'Arabic': 'ar', 'Chinese': 'zh-cn',
19
+ 'Hebrew': 'he', 'Hindi': 'hi', 'Italian': 'it', 'Japanese': 'ja', 'Korean': 'ko', 'Portuguese': 'pt'}
20
+ initial_prompt = "How can I help you today?"
21
+
22
+ def launch_bot():
23
+ def reset():
24
+ cfg = st.session_state.cfg
25
+ llm = OpenAI(model="gpt-4o", temperature=0)
26
+ tr_prompt = Translator(to_lang=languages[cfg.language]).translate(initial_prompt)
27
+ print(tr_prompt)
28
+ st.session_state.messages = [{"role": "assistant", "content": tr_prompt, "avatar": "πŸ¦–"}]
29
+ vectara = VectaraIndex(vectara_api_key=cfg.api_key,
30
+ vectara_customer_id=cfg.customer_id,
31
+ vectara_corpus_id=cfg.corpus_id)
32
+ vectara_tool = QueryEngineTool(
33
+ query_engine = vectara.as_query_engine(summary_enabled=True, summary_num_results=5, summary_response_lang = languages[cfg.language],
34
+ summary_prompt_name="vectara-summary-ext-24-05-large"),
35
+ metadata = ToolMetadata(name="Vectara",
36
+ description="Vectara Query Engine that is able to answer any questions about the Justice Harvard class."),
37
+ )
38
+ llm = OpenAI(model="gpt-4o", temperature=0)
39
+ st.session_state.agent = ReActAgent.from_tools(
40
+ tools=[vectara_tool], llm=llm,
41
+ context = f'''
42
+ You are a teacher assistant at Justice Harvard course. You are helping a student with his questions.
43
+ The student is student who is {cfg.student_age} years old, you personalize your assistance to the student's age,
44
+ and rephrase your answer if needed to fit the {cfg.style} learning style.
45
+ ''',
46
+ verbose=True
47
+ )
48
+
49
+ if 'cfg' not in st.session_state:
50
+ cfg = OmegaConf.create({
51
+ 'customer_id': str(os.environ['VECTARA_CUSTOMER_ID']),
52
+ 'corpus_id': str(os.environ['VECTARA_CORPUS_ID']),
53
+ 'api_key': str(os.environ['VECTARA_API_KEY']),
54
+ 'style': learning_styles[0],
55
+ 'language': 'English',
56
+ 'student_age': 21
57
+ })
58
+ st.session_state.cfg = cfg
59
+ st.session_state.style = learning_styles[0]
60
+ st.session_state.language = 'English'
61
+ st.session_state.student_age = 21
62
+ reset()
63
+
64
+ cfg = st.session_state.cfg
65
+ st.set_page_config(page_title="Teaching Assistant", layout="wide")
66
+
67
+ # left side content
68
+ with st.sidebar:
69
+ image = Image.open('Vectara-logo.png')
70
+ st.image(image, width=250)
71
+ st.markdown(f"## Welcome to Justice Harvard.\n\n\n")
72
+
73
+ st.markdown("\n")
74
+ cfg.style = st.selectbox('Learning Style:', learning_styles)
75
+ if st.session_state.style != cfg.style:
76
+ st.session_state.style = cfg.style
77
+ reset()
78
+
79
+ st.markdown("\n")
80
+ cfg.language = st.selectbox('Language:', languages.keys())
81
+ if st.session_state.language != cfg.language:
82
+ st.session_state.langage = cfg.language
83
+ reset()
84
+
85
+ st.markdown("\n")
86
+ cfg.student_age = st.number_input(
87
+ 'Student age:', min_value=13, value=cfg.student_age,
88
+ step=1, format='%i'
89
+ )
90
+ if st.session_state.student_age != cfg.student_age:
91
+ st.session_state.student_age = cfg.student_age
92
+ reset()
93
+
94
+ st.markdown("\n\n")
95
+ if st.button('Start Over'):
96
+ reset()
97
+
98
+ st.markdown("---")
99
+ st.markdown(
100
+ "## How this works?\n"
101
+ "This app was built with [Vectara](https://vectara.com).\n\n"
102
+ "It demonstrates the use of Agentic Chat functionality with Vectara"
103
+ )
104
+ st.markdown("---")
105
+
106
+
107
+ if "messages" not in st.session_state.keys():
108
+ reset()
109
+
110
+ # Display chat messages
111
+ for message in st.session_state.messages:
112
+ with st.chat_message(message["role"], avatar=message["avatar"]):
113
+ st.write(message["content"])
114
+
115
+ # User-provided prompt
116
+ if prompt := st.chat_input():
117
+ st.session_state.messages.append({"role": "user", "content": prompt, "avatar": 'πŸ§‘β€πŸ’»'})
118
+ with st.chat_message("user", avatar='πŸ§‘β€πŸ’»'):
119
+ print_text(f"Starting new question: {prompt}\n", color='green')
120
+ st.write(prompt)
121
+
122
+ # Generate a new response if last message is not from assistant
123
+ if st.session_state.messages[-1]["role"] != "assistant":
124
+ with st.chat_message("assistant", avatar='πŸ€–'):
125
+ with st.spinner('Thinking...'):
126
+ res = st.session_state.agent.chat(prompt)
127
+ cleaned = re.sub(r'\[\d+\]', '', res.response)
128
+ response = st.write(cleaned)
129
+
130
+ message = {"role": "assistant", "content": cleaned, "avatar": 'πŸ€–'}
131
+ st.session_state.messages.append(message)
132
+
133
+ if __name__ == "__main__":
134
+ launch_bot()
135
+
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ requests_to_curl==1.1.0
2
+ toml==0.10.2
3
+ omegaconf==2.3.0
4
+ syrupy==4.0.8
5
+ streamlit==1.32.2
6
+ translate==3.6.1