AdronX commited on
Commit
e894de7
Β·
verified Β·
1 Parent(s): 781b5a1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -35
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("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"))
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, "pages": text.count("[PAGE ")}
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({"role": "user", "content": question})
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 (in original language if present)
52
- 3. Always mention PAGE NUMBER using [PAGE X] markers in the text
53
  4. Provide English translation
54
- 5. Provide Tafseer/Commentary/Summary
55
- 6. Format response clearly:
56
 
57
  πŸ“– ORIGINAL TEXT (Page X):
58
- [exact text from book]
59
 
60
  🌐 TRANSLATION:
61
  [English translation]
62
 
63
  πŸ“ TAFSEER / COMMENTARY:
64
- [explanation and context]
65
 
66
  πŸ“Œ SOURCE: {book_name}, Page X
67
 
@@ -69,20 +67,25 @@ BOOK CONTENT:
69
  {book_text[:60000]}"""
70
 
71
  messages = []
72
- for msg in chat_history[-10:]:
73
- messages.append({"role": msg["role"], "content": msg["content"]})
 
 
 
74
  messages.append({"role": "user", "content": question})
75
 
76
- response = client.messages.create(
77
- model="claude-sonnet-4-20250514",
78
- max_tokens=1500,
79
- system=system_prompt,
80
- messages=messages
81
- )
 
 
 
 
82
 
83
- answer = response.content[0].text
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
- *Ask anything from any religious scripture β€” get exact quotes with page numbers, translations, and Tafseer*
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", "purpose of life", "Surah Al-Fatiha"',
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 here. Users will get exact quotes from your uploaded books.
154
- > Keep this Space **Private** on Hugging Face to restrict admin access.
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 β€” Built with Claude AI + Gradio + PyPDF2*")
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()