Spaces:
Sleeping
Sleeping
File size: 6,312 Bytes
5cc33fe 59b0b83 31d4cfe 1300b3c 3f7d704 6ea8ae1 214383a 93e61af 214383a 3f7d704 214383a 3f7d704 06304c5 3f7d704 06304c5 3f7d704 06304c5 3f7d704 06304c5 3f7d704 214383a 06304c5 3f7d704 d506247 3f7d704 214383a a263608 3f7d704 a263608 3f7d704 06304c5 3f7d704 214383a a263608 3f7d704 a263608 3f7d704 06304c5 3f7d704 a263608 3f7d704 a263608 3f7d704 06304c5 3f7d704 a263608 214383a a263608 214383a a263608 214383a a263608 214383a 3f7d704 214383a 2a081ad 214383a a263608 214383a 3f7d704 214383a a263608 3f7d704 a263608 3f7d704 a263608 214383a 8594d4d 214383a |
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 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
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:
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) |