Spaces:
Sleeping
Sleeping
import os | |
from together import Together | |
import PyPDF2 | |
client = Together(api_key=os.environ.get("TOGETHER_API_KEY")) | |
def get_pdf_content(pdf_path): | |
with open(pdf_path, "rb") as file: | |
reader = PyPDF2.PdfReader(file) | |
text = "" | |
for page in reader.pages: | |
text += page.extract_text() if page.extract_text() else "" | |
return text | |
def chatbot_response(user_input, pdf_path): | |
pdf_content = get_pdf_content(pdf_path) | |
full_input = f"{user_input}\n\n{pdf_content}" # Append PDF content to the user input | |
response = client.chat.completions.create( | |
model="meta-llama/Llama-3-8b-chat-hf", | |
messages=[{"role": "user", "content": full_input}], | |
) | |
return response.choices[0].message.content | |
if __name__ == "__main__": | |
pdf_path = "sample.pdf" # Adjust path as necessary | |
user_input = input("Hello! How can I assist you today?") | |
print(chatbot_response(user_input, pdf_path)) |