Jekyll2000 commited on
Commit
7e996c7
1 Parent(s): 2a20ce6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -59
app.py CHANGED
@@ -1,13 +1,4 @@
1
- from langchain_community.chat_message_histories import StreamlitChatMessageHistory
2
  import streamlit as st
3
- from langchain.prompts import (
4
- ChatPromptTemplate,
5
- HumanMessagePromptTemplate,
6
- MessagesPlaceholder,
7
- )
8
- from more_itertools import chunked
9
-
10
- from langserve import RemoteRunnable
11
  from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
12
  import os
13
  from langchain import PromptTemplate
@@ -15,17 +6,17 @@ from langchain import LLMChain
15
  from langchain_together import Together
16
  import re
17
  import pdfplumber
18
- # Set the API key with double quotes
19
 
 
20
  os.environ['TOGETHER_API_KEY'] = "5653bbfbaf1f7c1438206f18e5dfc2f5992b8f0b6aa9796b0131ea454648ccde"
21
 
22
  text = ""
23
  max_pages = 16
24
  with pdfplumber.open("AI Engineer Test.pdf") as pdf:
25
- for i, page in enumerate(pdf.pages):
26
- if i >= max_pages:
27
- break
28
- text += page.extract_text() + "\n"
29
 
30
  def Bot(Questions):
31
  chat_template = """
@@ -40,52 +31,42 @@ def Bot(Questions):
40
  llama3 = Together(model="meta-llama/Llama-3-70b-chat-hf", max_tokens=250)
41
  Generated_chat = LLMChain(llm=llama3, prompt=prompt)
42
 
43
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
  def ChatBot(Questions):
45
- greetings = ["hi", "hello", "hey", "greetings", "what's up", "howdy"]
46
  # Check if the input question is a greeting
47
- question_lower = Questions.lower().strip()
48
- if question_lower in greetings or any(question_lower.startswith(greeting) for greeting in greetings):
49
  return "Hello! How can I assist you with the document today?"
50
- else:
51
- response=Bot(Questions)
52
- return response.translate(str.maketrans('', '', '\n'))
53
- """
54
- # --- Logo ---
55
- st.set_page_config(
56
- page_title="AI Engineer Test Chatbot",
57
- page_icon="Insight Therapy Solutions.png",
58
- layout="wide",
59
- )
60
- st.sidebar.image("Insight Therapy Solutions.png", width=200)
61
 
62
- st.sidebar.title("Navigation")
63
- st.sidebar.write("Reclaim Your Mental Health")
64
- st.sidebar.markdown("[Visit us at](https://www.insighttherapysolutions.com/)")
65
- """
66
- # Add some custom styling
67
- st.markdown(
68
-
69
- <style>
70
- .css-18e3th9 {
71
- padding-top: 3rem;
72
- }
73
- .css-1d391kg {
74
- text-align: center;
75
- }
76
- .stButton>button {
77
- background-color: #4CAF50;
78
- color: white;
79
- border: none;
80
- padding: 15px 32px;
81
- text-align: center;
82
- text-decoration: none;
83
- display: inline-block;
84
- font-size: 16px;
85
- margin: 4px 2px;
86
- cursor: pointer;
87
- border-radius: 8px;
88
- }
89
- </style>
90
- , unsafe_allow_html=True
91
- )
 
 
1
  import streamlit as st
 
 
 
 
 
 
 
 
2
  from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
3
  import os
4
  from langchain import PromptTemplate
 
6
  from langchain_together import Together
7
  import re
8
  import pdfplumber
 
9
 
10
+ # Set the API key
11
  os.environ['TOGETHER_API_KEY'] = "5653bbfbaf1f7c1438206f18e5dfc2f5992b8f0b6aa9796b0131ea454648ccde"
12
 
13
  text = ""
14
  max_pages = 16
15
  with pdfplumber.open("AI Engineer Test.pdf") as pdf:
16
+ for i, page in enumerate(pdf.pages):
17
+ if i >= max_pages:
18
+ break
19
+ text += page.extract_text() + "\n"
20
 
21
  def Bot(Questions):
22
  chat_template = """
 
31
  llama3 = Together(model="meta-llama/Llama-3-70b-chat-hf", max_tokens=250)
32
  Generated_chat = LLMChain(llm=llama3, prompt=prompt)
33
 
34
+ try:
35
+ response = Generated_chat.invoke({
36
+ "text": text,
37
+ "Questions": Questions
38
+ })
39
+
40
+ response_text = response['text']
41
+
42
+ response_text = response_text.replace("assistant", "")
43
+
44
+ # Post-processing to handle repeated words and ensure completeness
45
+ words = response_text.split()
46
+ seen = set()
47
+ filtered_words = [word for word in words if word.lower() not in seen and not seen.add(word.lower())]
48
+ response_text = ' '.join(filtered_words)
49
+ response_text = response_text.strip() # Ensuring no extra spaces at the ends
50
+ if not response_text.endswith('.'):
51
+ response_text += '.'
52
+
53
+ return response_text
54
+ except Exception as e:
55
+ return f"Error in generating response: {e}"
56
+
57
  def ChatBot(Questions):
58
+ greetings = ["hi", "hello", "hey", "greetings", "what's up", "howdy"]
59
  # Check if the input question is a greeting
60
+ question_lower = Questions.lower().strip()
61
+ if question_lower in greetings or any(question_lower.startswith(greeting) for greeting in greetings):
62
  return "Hello! How can I assist you with the document today?"
63
+ else:
64
+ response = Bot(Questions)
65
+ return response.translate(str.maketrans('', '', '\n'))
 
 
 
 
 
 
 
 
66
 
67
+ # Streamlit UI
68
+ st.title("Chatbot")
69
+ Questions = st.text_input("Ask a question:")
70
+ if st.button("Submit"):
71
+ answer = ChatBot(Questions)
72
+ st.write(answer)