Spaces:
Sleeping
Sleeping
Commit
·
550dfbc
1
Parent(s):
61bff19
project
Browse files- New Data Set.pdf +0 -0
- README.md +5 -5
- app.py +96 -0
- requirements.txt +7 -0
New Data Set.pdf
ADDED
|
Binary file (241 kB). View file
|
|
|
README.md
CHANGED
|
@@ -1,10 +1,10 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version:
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
---
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Career Guidance Chat Bot
|
| 3 |
+
emoji: 🏢
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: yellow
|
| 6 |
sdk: gradio
|
| 7 |
+
sdk_version: 3.47.1
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
---
|
app.py
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 3 |
+
import os
|
| 4 |
+
from langchain import PromptTemplate
|
| 5 |
+
from langchain import LLMChain
|
| 6 |
+
from langchain_together import Together
|
| 7 |
+
import re
|
| 8 |
+
import pdfplumber
|
| 9 |
+
# Set the API key with double quotes
|
| 10 |
+
|
| 11 |
+
os.environ['TOGETHER_API_KEY'] = "d88cb7414e4039a84d2ed63f1b47daaaa4230c4c53a422045d8a30a9a3bc87d8"
|
| 12 |
+
|
| 13 |
+
text = ""
|
| 14 |
+
max_pages = 16
|
| 15 |
+
with pdfplumber.open("New Data Set.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 = """
|
| 23 |
+
Based on the provided context: {text}
|
| 24 |
+
Please answer the following question: {Questions}
|
| 25 |
+
|
| 26 |
+
Only provide answers that are directly related to the context. If the question is unrelated, respond with "I don't know".
|
| 27 |
+
"""
|
| 28 |
+
prompt = PromptTemplate(
|
| 29 |
+
input_variables=['text', 'Questions'],
|
| 30 |
+
template=chat_template
|
| 31 |
+
)
|
| 32 |
+
llama3 = Together(model="meta-llama/Llama-3-70b-chat-hf", max_tokens=50)
|
| 33 |
+
Generated_chat = LLMChain(llm=llama3, prompt=prompt)
|
| 34 |
+
|
| 35 |
+
try:
|
| 36 |
+
response = Generated_chat.invoke({
|
| 37 |
+
"text": text,
|
| 38 |
+
"Questions": Questions
|
| 39 |
+
})
|
| 40 |
+
|
| 41 |
+
response_text = response['text']
|
| 42 |
+
|
| 43 |
+
response_text = response_text.replace("assistant", "")
|
| 44 |
+
|
| 45 |
+
# Post-processing to handle repeated words and ensure completeness
|
| 46 |
+
words = response_text.split()
|
| 47 |
+
seen = set()
|
| 48 |
+
filtered_words = [word for word in words if word.lower() not in seen and not seen.add(word.lower())]
|
| 49 |
+
response_text = ' '.join(filtered_words)
|
| 50 |
+
response_text = response_text.strip() # Ensuring no extra spaces at the ends
|
| 51 |
+
if not response_text.endswith('.'):
|
| 52 |
+
response_text += '.'
|
| 53 |
+
|
| 54 |
+
return response_text
|
| 55 |
+
except Exception as e:
|
| 56 |
+
return f"Error in generating response: {e}"
|
| 57 |
+
|
| 58 |
+
def ChatBot(Questions):
|
| 59 |
+
greetings = ["hi", "hello", "hey", "greetings", "what's up", "howdy"]
|
| 60 |
+
# Check if the input question is a greeting
|
| 61 |
+
question_lower = Questions.lower().strip()
|
| 62 |
+
if question_lower in greetings or any(question_lower.startswith(greeting) for greeting in greetings):
|
| 63 |
+
return "Hello! How can I assist you with the document today?"
|
| 64 |
+
else:
|
| 65 |
+
response=Bot(Questions)
|
| 66 |
+
return response.translate(str.maketrans('', '', '\n'))
|
| 67 |
+
# text_embedding = model.encode(text, convert_to_tensor=True)
|
| 68 |
+
# statement_embedding = model.encode(statement, convert_to_tensor=True)
|
| 69 |
+
|
| 70 |
+
# # Compute the cosine similarity between the embeddings
|
| 71 |
+
# similarity = util.pytorch_cos_sim(text_embedding, statement_embedding)
|
| 72 |
+
|
| 73 |
+
# # Print the similarity score
|
| 74 |
+
# print(f"Cosine similarity: {similarity.item()}")
|
| 75 |
+
|
| 76 |
+
# # Define a threshold for considering the statement as related
|
| 77 |
+
# threshold = 0.7
|
| 78 |
+
|
| 79 |
+
# if similarity.item() > threshold:
|
| 80 |
+
# response=Bot(Questions)
|
| 81 |
+
# return response
|
| 82 |
+
# else:
|
| 83 |
+
# response="The statement is not related to the text."
|
| 84 |
+
# return response
|
| 85 |
+
|
| 86 |
+
iface = gr.Interface(fn=ChatBot, inputs="text", outputs="text", title="Chatbot")
|
| 87 |
+
iface.launch(debug=True)
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers
|
| 2 |
+
langchain_together
|
| 3 |
+
langchain
|
| 4 |
+
torch
|
| 5 |
+
nltk
|
| 6 |
+
sentencepiece
|
| 7 |
+
pdfplumber
|