peterciank commited on
Commit
7591ccd
·
verified ·
1 Parent(s): d25d788

Update pages/Phase1.py

Browse files
Files changed (1) hide show
  1. pages/Phase1.py +55 -44
pages/Phase1.py CHANGED
@@ -1,33 +1,23 @@
1
  import streamlit as st
2
  from huggingface_hub import InferenceClient
3
- import os
4
  import fitz # PyMuPDF
 
5
 
6
  st.title("ChatGPT-like Chatbot")
7
 
8
  base_url = "https://api-inference.huggingface.co/models/"
9
-
10
  API_KEY = os.environ.get('HUGGINGFACE_API_KEY')
11
  headers = {"Authorization": "Bearer " + str(API_KEY)}
12
 
13
  model_links = {
14
- "Mistral-7B": base_url + "mistralai/Mistral-7B-Instruct-v0.2",
15
- "Mistral-22B": base_url + "mistral-community/Mixtral-8x22B-v0.1",
16
  }
17
 
18
  model_info = {
19
  "Mistral-7B": {
20
- 'description': """The Mistral model is a **Large Language Model (LLM)** that's able to have question and answer interactions.\n \
21
- \nIt was created by the [**Mistral AI**](https://mistral.ai/news/announcing-mistral-7b/) team as has over **7 billion parameters.** \n""",
22
- 'logo': 'https://mistral.ai/images/logo_hubc88c4ece131b91c7cb753f40e9e1cc5_2589_256x0_resize_q97_h2_lanczos_3.webp'},
23
- "Zephyr-7B": {
24
- 'description': """The Zephyr model is a **Large Language Model (LLM)** that's able to have question and answer interactions.\n \
25
- \nFrom Huggingface: \n\
26
- Zephyr is a series of language models that are trained to act as helpful assistants. \
27
- [Zephyr 7B Gemma](https://huggingface.co/HuggingFaceH4/zephyr-7b-gemma-v0.1)\
28
- is the third model in the series, and is a fine-tuned version of google/gemma-7b \
29
- that was trained on on a mix of publicly available, synthetic datasets using Direct Preference Optimization (DPO)\n""",
30
- 'logo': 'https://huggingface.co/HuggingFaceH4/zephyr-7b-gemma-v0.1/resolve/main/thumbnail.png'}
31
  }
32
 
33
  def format_prompt(message, custom_instructions=None):
@@ -42,22 +32,31 @@ def reset_conversation():
42
  st.session_state.messages = []
43
  return None
44
 
45
- def read_pdf(file_path):
46
- doc = fitz.open(file_path)
47
  text = ""
48
- for page in doc:
 
49
  text += page.get_text()
50
  return text
51
 
52
  models = [key for key in model_links.keys()]
53
 
 
54
  selected_model = st.sidebar.selectbox("Select Model", models)
55
- temp_values = st.sidebar.slider('Select a temperature value', 0.0, 1.0, 0.5)
56
- st.sidebar.button('Reset Chat', on_click=reset_conversation)
57
- st.sidebar.write(f"You're now chatting with **{selected_model}**")
 
 
 
 
 
 
58
  st.sidebar.markdown(model_info[selected_model]['description'])
59
  st.sidebar.image(model_info[selected_model]['logo'])
60
- st.sidebar.markdown("*Generated content may be inaccurate or false.*")
 
61
 
62
  if "prev_option" not in st.session_state:
63
  st.session_state.prev_option = selected_model
@@ -67,44 +66,56 @@ if st.session_state.prev_option != selected_model:
67
  st.session_state.prev_option = selected_model
68
  reset_conversation()
69
 
 
70
  repo_id = model_links[selected_model]
 
71
  st.subheader(f'AI - {selected_model}')
 
72
 
 
73
  if "messages" not in st.session_state:
74
  st.session_state.messages = []
75
 
 
76
  for message in st.session_state.messages:
77
  with st.chat_message(message["role"]):
78
  st.markdown(message["content"])
79
 
80
- pdf_path = st.file_uploader("Upload a PDF file", type="pdf")
81
-
82
- if pdf_path:
83
- pdf_text = read_pdf(pdf_path)
 
 
84
 
 
85
  if prompt := st.chat_input(f"Hi I'm {selected_model}, ask me a question"):
 
86
  custom_instruction = "Act like a Human in conversation"
 
 
 
87
 
 
88
  with st.chat_message("user"):
89
  st.markdown(prompt)
90
- st.session_state.messages.append({"role": "user", "content": prompt})
 
91
 
92
- formatted_prompt = format_prompt(prompt, custom_instruction)
93
 
 
94
  with st.chat_message("assistant"):
95
- with st.spinner("Thinking..."):
96
- client = InferenceClient(model=repo_id, token=API_KEY)
97
-
98
- if pdf_path:
99
- # If a PDF is uploaded, use its text for answering
100
- formatted_prompt = f"{pdf_text}\n\n{formatted_prompt}"
101
-
102
- response = client.text_generation(
103
- formatted_prompt,
104
- max_length=500,
105
- temperature=temp_values
106
- )
107
-
108
- response_content = response["generated_text"]
109
- st.markdown(response_content)
110
- st.session_state.messages.append({"role": "assistant", "content": response_content})
 
1
  import streamlit as st
2
  from huggingface_hub import InferenceClient
 
3
  import fitz # PyMuPDF
4
+ import os
5
 
6
  st.title("ChatGPT-like Chatbot")
7
 
8
  base_url = "https://api-inference.huggingface.co/models/"
 
9
  API_KEY = os.environ.get('HUGGINGFACE_API_KEY')
10
  headers = {"Authorization": "Bearer " + str(API_KEY)}
11
 
12
  model_links = {
13
+ "Mistral-7B": base_url + "mistralai/Mistral-7B-Instruct-v0.2"
 
14
  }
15
 
16
  model_info = {
17
  "Mistral-7B": {
18
+ 'description': "Good Model",
19
+ 'logo': 'model.jpg'
20
+ }
 
 
 
 
 
 
 
 
21
  }
22
 
23
  def format_prompt(message, custom_instructions=None):
 
32
  st.session_state.messages = []
33
  return None
34
 
35
+ def read_pdf(file):
36
+ pdf_document = fitz.open(file)
37
  text = ""
38
+ for page_num in range(len(pdf_document)):
39
+ page = pdf_document[page_num]
40
  text += page.get_text()
41
  return text
42
 
43
  models = [key for key in model_links.keys()]
44
 
45
+ # Create the sidebar with the dropdown for model selection
46
  selected_model = st.sidebar.selectbox("Select Model", models)
47
+
48
+ # Create a temperature slider
49
+ temp_values = st.sidebar.slider('Select a temperature value', 0.0, 1.0, (0.5))
50
+
51
+ # Add reset button to clear conversation
52
+ st.sidebar.button('Reset Chat', on_click=reset_conversation) # Reset button
53
+
54
+ # Create model description
55
+ st.sidebar.write(f"You're now chatting with {selected_model}")
56
  st.sidebar.markdown(model_info[selected_model]['description'])
57
  st.sidebar.image(model_info[selected_model]['logo'])
58
+ st.sidebar.markdown("Generated content may be inaccurate or false.")
59
+ st.sidebar.markdown("\nLearn how to build this chatbot here.")
60
 
61
  if "prev_option" not in st.session_state:
62
  st.session_state.prev_option = selected_model
 
66
  st.session_state.prev_option = selected_model
67
  reset_conversation()
68
 
69
+ # Pull in the model we want to use
70
  repo_id = model_links[selected_model]
71
+
72
  st.subheader(f'AI - {selected_model}')
73
+ st.title(f'ChatBot Using {selected_model}')
74
 
75
+ # Initialize chat history
76
  if "messages" not in st.session_state:
77
  st.session_state.messages = []
78
 
79
+ # Display chat messages from history on app rerun
80
  for message in st.session_state.messages:
81
  with st.chat_message(message["role"]):
82
  st.markdown(message["content"])
83
 
84
+ # Upload PDF
85
+ uploaded_file = st.file_uploader("Choose a PDF file", type="pdf")
86
+ if uploaded_file is not None:
87
+ pdf_text = read_pdf(uploaded_file)
88
+ st.session_state.pdf_text = pdf_text
89
+ st.write("PDF content loaded successfully!")
90
 
91
+ # Accept user input
92
  if prompt := st.chat_input(f"Hi I'm {selected_model}, ask me a question"):
93
+
94
  custom_instruction = "Act like a Human in conversation"
95
+
96
+ if "pdf_text" in st.session_state:
97
+ prompt = f"{st.session_state.pdf_text}\n\nQuestion: {prompt}"
98
 
99
+ # Display user message in chat message container
100
  with st.chat_message("user"):
101
  st.markdown(prompt)
102
+ # Add user message to chat history
103
+ st.session_state.messages.append({"role": "user", "content": prompt})
104
 
105
+ formated_text = format_prompt(prompt, custom_instruction)
106
 
107
+ # Display assistant response in chat message container
108
  with st.chat_message("assistant"):
109
+ client = InferenceClient(
110
+ model=model_links[selected_model],
111
+ headers=headers)
112
+
113
+ output = client.text_generation(
114
+ formated_text,
115
+ temperature=temp_values, # 0.5
116
+ max_new_tokens=3000,
117
+ stream=True
118
+ )
119
+
120
+ response = st.write_stream(output)
121
+ st.session_state.messages.append({"role": "assistant", "content": response})