peterciank commited on
Commit
f0fa217
1 Parent(s): 012be20

Create pages/Phase1.py

Browse files
Files changed (1) hide show
  1. pages/Phase1.py +109 -0
pages/Phase1.py ADDED
@@ -0,0 +1,109 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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):
34
+ prompt = ""
35
+ if custom_instructions:
36
+ prompt += f"[INST] {custom_instructions} [/INST]"
37
+ prompt += f"[INST] {message} [/INST]"
38
+ return prompt
39
+
40
+ def reset_conversation():
41
+ st.session_state.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
64
+
65
+ if st.session_state.prev_option != selected_model:
66
+ st.session_state.messages = []
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 st.session_state.messages.append({"role": "user", "content": prompt})
90
+
91
+ formatted_prompt = format_prompt(prompt, custom_instruction)
92
+
93
+ with st.chat_message("assistant"):
94
+ with st.spinner("Thinking..."):
95
+ client = InferenceClient(model=repo_id, token=API_KEY)
96
+
97
+ if pdf_path:
98
+ # If a PDF is uploaded, use its text for answering
99
+ formatted_prompt = f"{pdf_text}\n\n{formatted_prompt}"
100
+
101
+ response = client.text_generation(
102
+ formatted_prompt,
103
+ max_length=500,
104
+ temperature=temp_values
105
+ )
106
+
107
+ response_content = response["generated_text"]
108
+ st.markdown(response_content)
109
+ st.session_state.messages.append({"role": "assistant", "content": response_content})