Spaces:
No application file
No application file
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,7 +3,7 @@ import anthropic
|
|
| 3 |
import PyPDF2
|
| 4 |
import os
|
| 5 |
|
| 6 |
-
client = anthropic.Anthropic(api_key=os.environ.get("
|
| 7 |
|
| 8 |
book_storage = {}
|
| 9 |
|
|
@@ -27,7 +27,7 @@ def upload_book(book_name, pdf_file):
|
|
| 27 |
return "Please select a PDF file."
|
| 28 |
text, msg = extract_pdf_text(pdf_file)
|
| 29 |
if text:
|
| 30 |
-
book_storage[book_name] = {"text": text
|
| 31 |
return f"β
'{book_name}' uploaded! {msg}"
|
| 32 |
return msg
|
| 33 |
|
|
@@ -36,32 +36,30 @@ def ask_question(book_name, question, chat_history):
|
|
| 36 |
return chat_history, ""
|
| 37 |
|
| 38 |
if book_name not in book_storage:
|
| 39 |
-
chat_history.append(
|
| 40 |
-
chat_history.append({"role": "assistant", "content": f"β οΈ Please upload '{book_name}' PDF first from the Admin Upload tab."})
|
| 41 |
return chat_history, ""
|
| 42 |
|
| 43 |
book_text = book_storage[book_name]["text"]
|
| 44 |
|
| 45 |
system_prompt = f"""You are a sacred scripture scholar for the AI Sacred Library.
|
| 46 |
-
|
| 47 |
-
You have the TEXT of: {book_name}
|
| 48 |
|
| 49 |
RULES:
|
| 50 |
-
1. Search the provided book text FIRST
|
| 51 |
-
2. Give EXACT original text from the book (
|
| 52 |
-
3. Always mention PAGE NUMBER
|
| 53 |
4. Provide English translation
|
| 54 |
-
5. Provide Tafseer/Commentary/Summary
|
| 55 |
-
6. Format response
|
| 56 |
|
| 57 |
π ORIGINAL TEXT (Page X):
|
| 58 |
-
[exact text
|
| 59 |
|
| 60 |
π TRANSLATION:
|
| 61 |
[English translation]
|
| 62 |
|
| 63 |
π TAFSEER / COMMENTARY:
|
| 64 |
-
[explanation
|
| 65 |
|
| 66 |
π SOURCE: {book_name}, Page X
|
| 67 |
|
|
@@ -69,20 +67,25 @@ BOOK CONTENT:
|
|
| 69 |
{book_text[:60000]}"""
|
| 70 |
|
| 71 |
messages = []
|
| 72 |
-
for
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
| 74 |
messages.append({"role": "user", "content": question})
|
| 75 |
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
-
|
| 84 |
-
chat_history.append({"role": "user", "content": question})
|
| 85 |
-
chat_history.append({"role": "assistant", "content": answer})
|
| 86 |
return chat_history, ""
|
| 87 |
|
| 88 |
BOOKS = [
|
|
@@ -90,22 +93,19 @@ BOOKS = [
|
|
| 90 |
"Tripitaka (Buddhism)", "The Vedas", "Guru Granth Sahib", "Avesta"
|
| 91 |
]
|
| 92 |
|
| 93 |
-
css = """
|
| 94 |
-
.gradio-container { max-width: 900px !important; margin: auto !important; }
|
| 95 |
-
"""
|
| 96 |
-
|
| 97 |
with gr.Blocks(title="Sacred Library β AI Scripture Database") as demo:
|
| 98 |
|
| 99 |
gr.Markdown("""
|
| 100 |
# π Sacred Library
|
| 101 |
### AI-Powered World Scripture Database
|
| 102 |
-
*
|
| 103 |
---
|
| 104 |
""")
|
| 105 |
|
| 106 |
with gr.Tabs():
|
| 107 |
|
| 108 |
with gr.Tab("π¬ Chat with Scripture"):
|
|
|
|
| 109 |
book_select = gr.Dropdown(
|
| 110 |
choices=BOOKS,
|
| 111 |
value="Al-Quran",
|
|
@@ -114,13 +114,12 @@ with gr.Blocks(title="Sacred Library β AI Scripture Database") as demo:
|
|
| 114 |
|
| 115 |
chatbot = gr.Chatbot(
|
| 116 |
label="Sacred Library AI",
|
| 117 |
-
height=450
|
| 118 |
-
type="messages"
|
| 119 |
)
|
| 120 |
|
| 121 |
with gr.Row():
|
| 122 |
question_input = gr.Textbox(
|
| 123 |
-
placeholder='e.g. "page 1", "what is karma", "
|
| 124 |
label="Your Question",
|
| 125 |
scale=4
|
| 126 |
)
|
|
@@ -150,8 +149,8 @@ with gr.Blocks(title="Sacred Library β AI Scripture Database") as demo:
|
|
| 150 |
with gr.Tab("π Admin β Upload Books"):
|
| 151 |
gr.Markdown("""
|
| 152 |
### Admin Book Upload Panel
|
| 153 |
-
Upload authentic scripture PDFs
|
| 154 |
-
> Keep this Space **Private** on Hugging Face to restrict
|
| 155 |
""")
|
| 156 |
|
| 157 |
with gr.Row():
|
|
@@ -176,6 +175,6 @@ with gr.Blocks(title="Sacred Library β AI Scripture Database") as demo:
|
|
| 176 |
outputs=[upload_status]
|
| 177 |
)
|
| 178 |
|
| 179 |
-
gr.Markdown("---\n*Sacred Library β
|
| 180 |
|
| 181 |
demo.launch()
|
|
|
|
| 3 |
import PyPDF2
|
| 4 |
import os
|
| 5 |
|
| 6 |
+
client = anthropic.Anthropic(api_key=os.environ.get("ANTHROPIC_API_KEY"))
|
| 7 |
|
| 8 |
book_storage = {}
|
| 9 |
|
|
|
|
| 27 |
return "Please select a PDF file."
|
| 28 |
text, msg = extract_pdf_text(pdf_file)
|
| 29 |
if text:
|
| 30 |
+
book_storage[book_name] = {"text": text}
|
| 31 |
return f"β
'{book_name}' uploaded! {msg}"
|
| 32 |
return msg
|
| 33 |
|
|
|
|
| 36 |
return chat_history, ""
|
| 37 |
|
| 38 |
if book_name not in book_storage:
|
| 39 |
+
chat_history.append((question, f"β οΈ Please upload '{book_name}' PDF first from the Admin Upload tab."))
|
|
|
|
| 40 |
return chat_history, ""
|
| 41 |
|
| 42 |
book_text = book_storage[book_name]["text"]
|
| 43 |
|
| 44 |
system_prompt = f"""You are a sacred scripture scholar for the AI Sacred Library.
|
| 45 |
+
You have the full TEXT of: {book_name}
|
|
|
|
| 46 |
|
| 47 |
RULES:
|
| 48 |
+
1. Search the provided book text FIRST before answering
|
| 49 |
+
2. Give EXACT original text from the book (keep original language like Arabic, Sanskrit, Hebrew)
|
| 50 |
+
3. Always mention PAGE NUMBER β use the [PAGE X] markers in the text
|
| 51 |
4. Provide English translation
|
| 52 |
+
5. Provide Tafseer / Commentary / Summary
|
| 53 |
+
6. Format your response like this:
|
| 54 |
|
| 55 |
π ORIGINAL TEXT (Page X):
|
| 56 |
+
[exact text]
|
| 57 |
|
| 58 |
π TRANSLATION:
|
| 59 |
[English translation]
|
| 60 |
|
| 61 |
π TAFSEER / COMMENTARY:
|
| 62 |
+
[explanation]
|
| 63 |
|
| 64 |
π SOURCE: {book_name}, Page X
|
| 65 |
|
|
|
|
| 67 |
{book_text[:60000]}"""
|
| 68 |
|
| 69 |
messages = []
|
| 70 |
+
for human, assistant in (chat_history or [])[-5:]:
|
| 71 |
+
if human:
|
| 72 |
+
messages.append({"role": "user", "content": human})
|
| 73 |
+
if assistant:
|
| 74 |
+
messages.append({"role": "assistant", "content": assistant})
|
| 75 |
messages.append({"role": "user", "content": question})
|
| 76 |
|
| 77 |
+
try:
|
| 78 |
+
response = client.messages.create(
|
| 79 |
+
model="claude-sonnet-4-20250514",
|
| 80 |
+
max_tokens=1500,
|
| 81 |
+
system=system_prompt,
|
| 82 |
+
messages=messages
|
| 83 |
+
)
|
| 84 |
+
answer = response.content[0].text
|
| 85 |
+
except Exception as e:
|
| 86 |
+
answer = f"β API Error: {str(e)}"
|
| 87 |
|
| 88 |
+
chat_history.append((question, answer))
|
|
|
|
|
|
|
| 89 |
return chat_history, ""
|
| 90 |
|
| 91 |
BOOKS = [
|
|
|
|
| 93 |
"Tripitaka (Buddhism)", "The Vedas", "Guru Granth Sahib", "Avesta"
|
| 94 |
]
|
| 95 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
with gr.Blocks(title="Sacred Library β AI Scripture Database") as demo:
|
| 97 |
|
| 98 |
gr.Markdown("""
|
| 99 |
# π Sacred Library
|
| 100 |
### AI-Powered World Scripture Database
|
| 101 |
+
*Exact quotes Β· Page numbers Β· Translation Β· Tafseer*
|
| 102 |
---
|
| 103 |
""")
|
| 104 |
|
| 105 |
with gr.Tabs():
|
| 106 |
|
| 107 |
with gr.Tab("π¬ Chat with Scripture"):
|
| 108 |
+
|
| 109 |
book_select = gr.Dropdown(
|
| 110 |
choices=BOOKS,
|
| 111 |
value="Al-Quran",
|
|
|
|
| 114 |
|
| 115 |
chatbot = gr.Chatbot(
|
| 116 |
label="Sacred Library AI",
|
| 117 |
+
height=450
|
|
|
|
| 118 |
)
|
| 119 |
|
| 120 |
with gr.Row():
|
| 121 |
question_input = gr.Textbox(
|
| 122 |
+
placeholder='e.g. "page 1", "what is karma", "Surah Al-Fatiha", "Genesis chapter 1"',
|
| 123 |
label="Your Question",
|
| 124 |
scale=4
|
| 125 |
)
|
|
|
|
| 149 |
with gr.Tab("π Admin β Upload Books"):
|
| 150 |
gr.Markdown("""
|
| 151 |
### Admin Book Upload Panel
|
| 152 |
+
Upload authentic scripture PDFs. Users get exact quotes from your books.
|
| 153 |
+
> π‘ Keep this Space **Private** on Hugging Face to restrict access.
|
| 154 |
""")
|
| 155 |
|
| 156 |
with gr.Row():
|
|
|
|
| 175 |
outputs=[upload_status]
|
| 176 |
)
|
| 177 |
|
| 178 |
+
gr.Markdown("---\n*Sacred Library β Claude AI + Gradio + PyPDF2*")
|
| 179 |
|
| 180 |
demo.launch()
|