Spaces:
Sleeping
Sleeping
import os | |
import gradio as gr | |
import requests | |
import json | |
# Retrieve the API token from secrets | |
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.") | |
# Use the token in your request headers | |
API_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.3" # Corrected URL | |
HEADERS = {"Authorization": f"Bearer {api_token}"} | |
def lookup_passage(passage): | |
# Placeholder implementation | |
return f"Text for {passage}" | |
def generate_exegesis(passage): | |
if not passage.strip(): | |
return "Please enter a Bible passage." | |
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>""" | |
payload = { | |
"inputs": prompt, | |
} | |
try: | |
response = requests.post(API_URL, headers=HEADERS, json=payload) | |
response.raise_for_status() | |
result = response.json() | |
print("Full API Response:", json.dumps(result, indent=4)) # Debug output | |
if isinstance(result, list) and len(result) > 0: | |
generated_text = result[0].get("generated_text", "") | |
marker = "Exegesis:" # Marker to split on | |
if marker in generated_text: | |
# Return only the text after the marker | |
generated_text = generated_text.split(marker, 1)[1].strip() | |
return generated_text or "Error: No response from model." | |
else: | |
return "Error: Unexpected response format." | |
except requests.exceptions.RequestException as e: | |
return f"API Error: {e}" | |
def ask_any_questions(question): | |
prompt = f"""<s>[INST] You are a professional Bible Scholar. Provide a detailed answer to the following question, including: | |
Relevant Bible verses, their explanations, and theological significance for: | |
{question} [/INST] Answer:</s>""" | |
payload = { | |
"inputs": prompt, | |
} | |
try: | |
response = requests.post(API_URL, headers=HEADERS, json=payload) | |
response.raise_for_status() | |
result = response.json() | |
print("Full API Response:", json.dumps(result, indent=4)) # Debug output | |
if isinstance(result, list) and len(result) > 0: | |
answer_text = result[0].get("generated_text", "") | |
marker = "Answer:" # Marker to split on | |
if marker in answer_text: | |
# Return only the text after the marker | |
answer_text = answer_text.split(marker, 1)[1].strip() | |
return answer_text or "Error: No response from model." | |
else: | |
return "Error: Unexpected response format." | |
except requests.exceptions.RequestException as e: | |
return f"API Error: {e}" | |
def generate_sermon(topic): | |
prompt = f"""<s>[INST] You are a professional Bible Scholar and Pastor. Provide a detailed sermon on the following topic, including: | |
Relevant Bible verses, their explanations, theological significance, and practical application for: | |
{topic} [/INST] Sermon:</s>""" | |
payload = { | |
"inputs": prompt, | |
} | |
try: | |
response = requests.post(API_URL, headers=HEADERS, json=payload) | |
response.raise_for_status() | |
result = response.json() | |
print("Full API Response:", json.dumps(result, indent=4)) # Debug output | |
if isinstance(result, list) and len(result) > 0: | |
sermon_text = result[0].get("generated_text", "") | |
marker = "Sermon:" # Marker to split on | |
if marker in sermon_text: | |
# Return only the text after the marker | |
sermon_text = sermon_text.split(marker, 1)[1].strip() | |
return sermon_text or "Error: No response from model." | |
else: | |
return "Error: Unexpected response format." | |
except requests.exceptions.RequestException as e: | |
return f"API Error: {e}" | |
def keyword_search(keyword): | |
prompt = f"""<s>[INST] You are a professional Bible Scholar. Search for the following keyword in the Bible and provide relevant passages, including: | |
The original Greek or Hebrew text, transliteration, and translation, as well as an exegesis of the keyword for: | |
{keyword} [/INST] Search Results:</s>""" | |
payload = { | |
"inputs": prompt, | |
} | |
try: | |
response = requests.post(API_URL, headers=HEADERS, json=payload) | |
response.raise_for_status() | |
result = response.json() | |
print("Full API Response:", json.dumps(result, indent=4)) # Debug output | |
if isinstance(result, list) and len(result) > 0: | |
search_results = result[0].get("generated_text", "") | |
marker = "Search Results:" # Marker to split on | |
if marker in search_results: | |
# Return only the text after the marker | |
search_results = search_results.split(marker, 1)[1].strip() | |
return search_results or "Error: No response from model." | |
else: | |
return "Error: Unexpected response format." | |
except requests.exceptions.RequestException as e: | |
return f"API Error: {e}" | |
# Gradio interface | |
exegesis_demo = gr.Interface( | |
fn=generate_exegesis, | |
inputs=gr.Textbox(label="Enter Bible Passage for Exegesis", placeholder="e.g., John 3:16"), | |
outputs=gr.Textbox(label="Exegesis Commentary"), | |
title="JR Study Bible", | |
description="Enter a Bible passage to receive insightful exegesis commentary." | |
) | |
lookup_demo = gr.Interface( | |
fn=lookup_passage, | |
inputs=gr.Textbox(label="Lookup Bible Passage", placeholder="e.g., John 3:16"), | |
outputs=gr.Textbox(label="Passage Text"), | |
title="Bible Passage Lookup", | |
description="Enter a Bible passage to lookup its text." | |
) | |
commentary_demo = gr.Interface( | |
fn=ask_any_questions, | |
inputs=gr.Textbox(label="Ask a Question", placeholder="e.g., What does John 3:16 mean?"), | |
outputs=gr.Textbox(label="Answer"), | |
title="Bible Commentary Generator", | |
description="Ask a question to generate commentary." | |
) | |
search_demo = gr.Interface( | |
fn=keyword_search, | |
inputs=gr.Textbox(label="Keyword Search", placeholder="e.g., love"), | |
outputs=gr.Textbox(label="Search Results"), | |
title="Bible Keyword Search", | |
description="Enter a keyword to search in the Bible." | |
) | |
# Combine all interfaces into one app | |
app = gr.TabbedInterface( | |
[exegesis_demo, lookup_demo, commentary_demo, search_demo], | |
["Exegesis", "Passage Lookup", "Commentary", "Keyword Search"] | |
) | |
if __name__ == "__main__": | |
app.launch() |