Spaces:
Runtime error
Runtime error
update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
GNU nano 8.3 ossy1.py
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
import json
|
|
@@ -19,6 +18,159 @@ def answer_bible_question(question, temperature=0.7, max_tokens=1000):
|
|
| 19 |
Send a Bible question to the DeepSeek model via OpenRouter API
|
| 20 |
and return the response.
|
| 21 |
"""
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
import json
|
|
|
|
| 18 |
Send a Bible question to the DeepSeek model via OpenRouter API
|
| 19 |
and return the response.
|
| 20 |
"""
|
| 21 |
+
if not OPENROUTER_API_KEY:
|
| 22 |
+
return "Error: API key not configured. Please set the OPENROUTER_API_KEY environment variable."
|
| 23 |
+
|
| 24 |
+
# Format the prompt to focus on ESV Bible information
|
| 25 |
+
prompt = f"""You are a helpful ESV Bible assistant. Please answer the following question
|
| 26 |
+
about the ESV Bible, providing relevant verses and explanations when appropriate: {question}"""
|
| 27 |
+
|
| 28 |
+
try:
|
| 29 |
+
response = requests.post(
|
| 30 |
+
url="https://openrouter.ai/api/v1/chat/completions",
|
| 31 |
+
headers={
|
| 32 |
+
"Authorization": f"Bearer {OPENROUTER_API_KEY}",
|
| 33 |
+
"Content-Type": "application/json",
|
| 34 |
+
"HTTP-Referer": "https://bible-assistant-app.com", # Replace with your site
|
| 35 |
+
"X-Title": "ESV Bible Assistant",
|
| 36 |
+
},
|
| 37 |
+
data=json.dumps({
|
| 38 |
+
"model": "tngtech/deepseek-r1t-chimera:free",
|
| 39 |
+
"messages": [
|
| 40 |
+
{
|
| 41 |
+
"role": "system",
|
| 42 |
+
"content": "You are a helpful assistant that specializes in the ESV Bible. Provide accurate information, quote relevant verses when appropriate, and explain biblical concepts clearly."
|
| 43 |
+
},
|
| 44 |
+
{
|
| 45 |
+
"role": "user",
|
| 46 |
+
"content": prompt
|
| 47 |
+
}
|
| 48 |
+
],
|
| 49 |
+
"temperature": temperature,
|
| 50 |
+
"max_tokens": max_tokens
|
| 51 |
+
}),
|
| 52 |
+
timeout=60
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
response_json = response.json()
|
| 56 |
+
|
| 57 |
+
if "error" in response_json:
|
| 58 |
+
return f"Error: {response_json['error']}"
|
| 59 |
+
|
| 60 |
+
return response_json["choices"][0]["message"]["content"]
|
| 61 |
+
|
| 62 |
+
except requests.exceptions.RequestException as e:
|
| 63 |
+
return f"Network error: {str(e)}"
|
| 64 |
+
except json.JSONDecodeError:
|
| 65 |
+
return f"Error parsing response: {response.text[:100]}..."
|
| 66 |
+
except Exception as e:
|
| 67 |
+
return f"Unexpected error: {str(e)}"
|
| 68 |
+
|
| 69 |
+
# Create custom theme for better aesthetics
|
| 70 |
+
custom_theme = gr.themes.Soft().set(
|
| 71 |
+
body_background_fill="#f8f9fa",
|
| 72 |
+
block_background_fill="#ffffff",
|
| 73 |
+
block_label_background_fill="#e9ecef",
|
| 74 |
+
button_primary_background_fill="#6c757d",
|
| 75 |
+
button_primary_background_fill_hover="#495057",
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
# Create the Gradio interface with dual-panel layout
|
| 79 |
+
with gr.Blocks(title="ESV Bible Assistant", theme=custom_theme) as demo:
|
| 80 |
+
gr.Markdown("""
|
| 81 |
+
# ESV Bible Assistant
|
| 82 |
+
|
| 83 |
+
Ask any question about the ESV Bible, and get answers powered by DeepSeek AI.
|
| 84 |
+
""")
|
| 85 |
+
|
| 86 |
+
with gr.Row(equal_height=True):
|
| 87 |
+
# Left panel - Questions
|
| 88 |
+
with gr.Column():
|
| 89 |
+
gr.Markdown("### Your Question")
|
| 90 |
+
question_input = gr.Textbox(
|
| 91 |
+
placeholder="Example: What does John 3:16 say in the ESV?",
|
| 92 |
+
lines=8,
|
| 93 |
+
label="",
|
| 94 |
+
elem_id="question-input"
|
| 95 |
+
)
|
| 96 |
+
|
| 97 |
+
with gr.Accordion("Settings", open=False):
|
| 98 |
+
temperature_slider = gr.Slider(
|
| 99 |
+
minimum=0.1,
|
| 100 |
+
maximum=1.0,
|
| 101 |
+
value=0.7,
|
| 102 |
+
step=0.1,
|
| 103 |
+
label="Temperature (Higher = more creative)"
|
| 104 |
+
)
|
| 105 |
+
|
| 106 |
+
max_tokens_slider = gr.Slider(
|
| 107 |
+
minimum=100,
|
| 108 |
+
maximum=4000,
|
| 109 |
+
value=1000,
|
| 110 |
+
step=100,
|
| 111 |
+
label="Maximum Response Length"
|
| 112 |
+
)
|
| 113 |
+
|
| 114 |
+
submit_btn = gr.Button("Ask", variant="primary", size="lg")
|
| 115 |
+
|
| 116 |
+
gr.Markdown("### Example Questions")
|
| 117 |
+
# Fixed Examples implementation
|
| 118 |
+
examples = gr.Examples(
|
| 119 |
+
examples=[
|
| 120 |
+
"What does John 3:16 say in the ESV?",
|
| 121 |
+
"Explain the concept of grace in the New Testament.",
|
| 122 |
+
"What are the fruits of the Spirit mentioned in Galatians?",
|
| 123 |
+
"Compare the creation accounts in Genesis 1 and 2.",
|
| 124 |
+
"What does the ESV Bible say about forgiveness?"
|
| 125 |
+
],
|
| 126 |
+
inputs=question_input
|
| 127 |
+
)
|
| 128 |
+
|
| 129 |
+
# Right panel - Answers
|
| 130 |
+
with gr.Column():
|
| 131 |
+
gr.Markdown("### Bible Answer")
|
| 132 |
+
answer_output = gr.Markdown(
|
| 133 |
+
value="Ask a question to receive an answer about the ESV Bible.",
|
| 134 |
+
elem_id="answer-output"
|
| 135 |
+
)
|
| 136 |
+
|
| 137 |
+
# Set up the click event
|
| 138 |
+
submit_btn.click(
|
| 139 |
+
fn=answer_bible_question,
|
| 140 |
+
inputs=[question_input, temperature_slider, max_tokens_slider],
|
| 141 |
+
outputs=answer_output
|
| 142 |
+
)
|
| 143 |
+
|
| 144 |
+
# Add custom CSS for better styling
|
| 145 |
+
gr.HTML("""
|
| 146 |
+
<style>
|
| 147 |
+
#question-input, #answer-output {
|
| 148 |
+
border: 1px solid #dee2e6;
|
| 149 |
+
border-radius: 0.375rem;
|
| 150 |
+
padding: 1rem;
|
| 151 |
+
min-height: 250px;
|
| 152 |
+
background-color: white;
|
| 153 |
+
}
|
| 154 |
+
.gradio-container {
|
| 155 |
+
max-width: 1200px !important;
|
| 156 |
+
margin-left: auto;
|
| 157 |
+
margin-right: auto;
|
| 158 |
+
}
|
| 159 |
+
footer {
|
| 160 |
+
text-align: center;
|
| 161 |
+
margin-top: 2rem;
|
| 162 |
+
color: #6c757d;
|
| 163 |
+
}
|
| 164 |
+
</style>
|
| 165 |
+
""")
|
| 166 |
+
|
| 167 |
+
gr.HTML("""
|
| 168 |
+
<footer>
|
| 169 |
+
<p>ESV Bible Assistant © 2025 | Powered by DeepSeek through OpenRouter</p>
|
| 170 |
+
<p><small>The ESV Bible text is copyrighted, but this application falls under fair use for educational purposes.</small></p>
|
| 171 |
+
</footer>
|
| 172 |
+
""")
|
| 173 |
+
|
| 174 |
+
# Launch the app
|
| 175 |
+
if __name__ == "__main__":
|
| 176 |
+
demo.launch()
|