Spaces:
Sleeping
Sleeping
import os | |
import gradio as gr | |
import requests | |
import json | |
import logging | |
from datetime import datetime | |
import random | |
# Configure logging | |
logging.basicConfig(level=logging.INFO) | |
logger = logging.getLogger(__name__) | |
# API configuration | |
api_token = os.getenv("API_TOKEN") | |
if not api_token: | |
raise ValueError("API token not found. Make sure 'API_TOKEN' is set in the Secrets.") | |
API_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.3" | |
HEADERS = {"Authorization": f"Bearer {api_token}"} | |
def get_unique_parameters(): | |
"""Generate unique parameters for each request""" | |
return { | |
"temperature": random.uniform(0.7, 0.9), | |
"top_p": random.uniform(0.85, 0.95), | |
"timestamp": datetime.now().strftime("%H%M%S"), | |
"style": random.choice([ | |
"analytical", "theological", "pastoral", "historical", | |
"contextual", "linguistic", "practical" | |
]) | |
} | |
def make_api_call(prompt, params): | |
"""Unified API call handler""" | |
payload = { | |
"inputs": f"{prompt} [ts:{params['timestamp']}]", | |
"parameters": { | |
"temperature": params["temperature"], | |
"top_p": params["top_p"] | |
} | |
} | |
try: | |
response = requests.post(API_URL, headers=HEADERS, json=payload) | |
response.raise_for_status() | |
return response.json() | |
except Exception as e: | |
logger.error(f"API Error: {e}") | |
return None | |
def process_response(result, marker): | |
"""Process API response""" | |
if not result or not isinstance(result, list) or len(result) == 0: | |
return "Error: Invalid response from model." | |
text = result[0].get("generated_text", "") | |
if marker in text: | |
return text.split(marker, 1)[1].strip() | |
return text | |
def generate_exegesis(passage): | |
if not passage.strip(): | |
return "Please enter a Bible passage." | |
params = get_unique_parameters() | |
prompt = f"""<s>[INST] You are a professional Bible Scholar. Provide a detailed exegesis of the following biblical verse, including: | |
Display the verses in KJV and ESV and then display The original Greek text and transliteration with word-by-word analysis and meanings, historical and cultural context, and theological significance for: | |
{passage} [/INST] Exegesis:</s>""" | |
result = make_api_call(prompt, params) | |
return process_response(result, "Exegesis:") | |
def ask_any_questions(question): | |
if not question.strip(): | |
return "Please enter a question." | |
params = get_unique_parameters() | |
prompt = f"""<s>[INST] As a Bible Scholar taking a {params['style']} perspective, answer: | |
{question} | |
Provide: | |
1. Clear explanation | |
2. Biblical references | |
3. Key insights | |
4. Application | |
[/INST] Answer:</s>""" | |
result = make_api_call(prompt, params) | |
return process_response(result, "Answer:") | |
def generate_sermon(topic): | |
if not topic.strip(): | |
return "Please enter a topic." | |
params = get_unique_parameters() | |
prompt = f"""<s>[INST] As a Pastor using a {params['style']} approach, create a sermon on: | |
{topic} | |
Include: | |
1. Main theme | |
2. Biblical foundation | |
3. Key points | |
4. Applications | |
[/INST] Sermon:</s>""" | |
result = make_api_call(prompt, params) | |
return process_response(result, "Sermon:") | |
def keyword_search(keyword): | |
if not keyword.strip(): | |
return "Please enter a keyword." | |
params = get_unique_parameters() | |
prompt = f"""<s>[INST] Using a {params['style']} method, search for passages about: | |
{keyword} | |
For each passage provide: | |
1. Reference | |
2. Context | |
3. Meaning | |
4. Application | |
[/INST] Search Results:</s>""" | |
result = make_api_call(prompt, params) | |
return process_response(result, "Search Results:") | |
# Interface styling | |
css = """ | |
.gradio-container { | |
font-family: 'Arial', sans-serif !important; | |
max-width: 1200px !important; | |
margin: auto !important; | |
} | |
.gr-button { | |
background-color: #2e5090 !important; | |
color: white !important; | |
} | |
.gr-input { | |
border: 2px solid #ddd !important; | |
border-radius: 8px !important; | |
} | |
""" | |
# Create interface | |
with gr.Blocks(css=css, theme=gr.themes.Default()) as bible_app: | |
gr.Markdown("# JR Study Bible") | |
with gr.Tabs(): | |
with gr.Tab("Exegesis"): | |
gr.Interface( | |
fn=generate_exegesis, | |
inputs=gr.Textbox( | |
label="Enter Bible Passage", | |
placeholder="e.g., John 3:16", | |
lines=2 | |
), | |
outputs=gr.Textbox( | |
label="Exegesis Commentary", | |
lines=12 | |
), | |
description="Enter a Bible passage for analysis" | |
) | |
with gr.Tab("Bible Q&A"): | |
gr.Interface( | |
fn=ask_any_questions, | |
inputs=gr.Textbox( | |
label="Ask Any Bible Question", | |
placeholder="e.g., What does John 3:16 mean?", | |
lines=2 | |
), | |
outputs=gr.Textbox( | |
label="Answer", | |
lines=12 | |
), | |
description="Ask any Bible-related question" | |
) | |
with gr.Tab("Sermon Generator"): | |
gr.Interface( | |
fn=generate_sermon, | |
inputs=gr.Textbox( | |
label="Generate Sermon", | |
placeholder="e.g., Faith, Love, Forgiveness", | |
lines=2 | |
), | |
outputs=gr.Textbox( | |
label="Sermon Outline", | |
lines=15 | |
), | |
description="Generate a sermon outline" | |
) | |
with gr.Tab("Bible Search"): | |
gr.Interface( | |
fn=keyword_search, | |
inputs=gr.Textbox( | |
label="Search the Bible", | |
placeholder="e.g., love, faith, hope", | |
lines=1 | |
), | |
outputs=gr.Textbox( | |
label="Search Results", | |
lines=12 | |
), | |
description="Search Bible passages by keyword" | |
) | |
if __name__ == "__main__": | |
bible_app.launch(share=True) |