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(""" """) gr.HTML(""" """) # Launch the app if __name__ == "__main__": demo.launch()