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