Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,13 +1,11 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import pdfplumber
|
| 3 |
-
import
|
| 4 |
import re
|
| 5 |
import unicodedata
|
| 6 |
-
import os
|
| 7 |
-
|
| 8 |
-
# Set up OpenAI API Key (Replace with your actual key)
|
| 9 |
-
openai.api_key = "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"
|
| 10 |
|
|
|
|
|
|
|
| 11 |
|
| 12 |
def clean_text(text):
|
| 13 |
"""Cleans extracted text for better processing by the model."""
|
|
@@ -28,42 +26,40 @@ def extract_text_from_pdf(pdf_file):
|
|
| 28 |
return None
|
| 29 |
|
| 30 |
def split_text(text, chunk_size=500):
|
| 31 |
-
"""Splits text into smaller chunks for
|
| 32 |
-
|
| 33 |
-
for i in range(0, len(text), chunk_size):
|
| 34 |
-
chunks.append(text[i:i+chunk_size])
|
| 35 |
-
return chunks
|
| 36 |
|
| 37 |
def chatbot(pdf_file, user_question):
|
| 38 |
"""Processes the PDF and answers the user's question."""
|
| 39 |
|
| 40 |
-
#
|
| 41 |
text = extract_text_from_pdf(pdf_file)
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
#
|
| 44 |
chunks = split_text(text)
|
| 45 |
-
|
| 46 |
-
# Step 3: Use only the first chunk for now (to reduce token usage)
|
| 47 |
-
if not chunks:
|
| 48 |
-
return "Could not extract any text from the PDF."
|
| 49 |
|
|
|
|
| 50 |
prompt = f"Based on this document, answer the question:\n\nDocument:\n{chunks[0]}\n\nQuestion: {user_question}"
|
| 51 |
|
| 52 |
-
#
|
| 53 |
-
response =
|
| 54 |
-
model="
|
| 55 |
-
|
|
|
|
|
|
|
| 56 |
)
|
| 57 |
|
| 58 |
-
#
|
| 59 |
-
return response["choices"][0]["
|
| 60 |
|
| 61 |
# Gradio Interface
|
| 62 |
iface = gr.Interface(
|
| 63 |
fn=chatbot,
|
| 64 |
inputs=[gr.File(label="Upload PDF"), gr.Textbox(label="Ask a Question")],
|
| 65 |
outputs=gr.Textbox(label="Answer"),
|
| 66 |
-
title="PDF Q&A Chatbot"
|
| 67 |
)
|
| 68 |
|
| 69 |
# Launch Gradio app
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import pdfplumber
|
| 3 |
+
import together
|
| 4 |
import re
|
| 5 |
import unicodedata
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
# Set up Together.AI API Key (Replace with your actual key)
|
| 8 |
+
together.api_key = "your_together_ai_api_key"
|
| 9 |
|
| 10 |
def clean_text(text):
|
| 11 |
"""Cleans extracted text for better processing by the model."""
|
|
|
|
| 26 |
return None
|
| 27 |
|
| 28 |
def split_text(text, chunk_size=500):
|
| 29 |
+
"""Splits text into smaller chunks for better processing."""
|
| 30 |
+
return [text[i:i+chunk_size] for i in range(0, len(text), chunk_size)]
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
def chatbot(pdf_file, user_question):
|
| 33 |
"""Processes the PDF and answers the user's question."""
|
| 34 |
|
| 35 |
+
# Extract text from the PDF
|
| 36 |
text = extract_text_from_pdf(pdf_file)
|
| 37 |
+
if not text:
|
| 38 |
+
return "Could not extract any text from the PDF."
|
| 39 |
|
| 40 |
+
# Split into smaller chunks
|
| 41 |
chunks = split_text(text)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
+
# Use only the first chunk (to optimize token usage)
|
| 44 |
prompt = f"Based on this document, answer the question:\n\nDocument:\n{chunks[0]}\n\nQuestion: {user_question}"
|
| 45 |
|
| 46 |
+
# Send to Together.AI (Mistral-7B)
|
| 47 |
+
response = together.Completion.create(
|
| 48 |
+
model="mistralai/Mistral-7B-Instruct-v0.1",
|
| 49 |
+
prompt=prompt,
|
| 50 |
+
max_tokens=200,
|
| 51 |
+
temperature=0.7,
|
| 52 |
)
|
| 53 |
|
| 54 |
+
# Return chatbot's response
|
| 55 |
+
return response["choices"][0]["text"]
|
| 56 |
|
| 57 |
# Gradio Interface
|
| 58 |
iface = gr.Interface(
|
| 59 |
fn=chatbot,
|
| 60 |
inputs=[gr.File(label="Upload PDF"), gr.Textbox(label="Ask a Question")],
|
| 61 |
outputs=gr.Textbox(label="Answer"),
|
| 62 |
+
title="PDF Q&A Chatbot (Powered by Together.AI)"
|
| 63 |
)
|
| 64 |
|
| 65 |
# Launch Gradio app
|