Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,7 @@ import os
|
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
import json
|
|
|
|
| 5 |
|
| 6 |
# Retrieve the API token from secrets
|
| 7 |
api_token = os.getenv("API_TOKEN")
|
|
@@ -9,11 +10,13 @@ if not api_token:
|
|
| 9 |
raise ValueError("API token not found. Make sure 'API_TOKEN' is set in the Secrets.")
|
| 10 |
|
| 11 |
# Use the token in your request headers
|
| 12 |
-
API_URL = "https://api-inference.huggingface.co/models/
|
| 13 |
HEADERS = {"Authorization": f"Bearer {api_token}"}
|
| 14 |
|
| 15 |
-
#
|
|
|
|
| 16 |
|
|
|
|
| 17 |
def generate_exegesis(passage):
|
| 18 |
if not passage.strip():
|
| 19 |
return "Please enter a Bible passage."
|
|
@@ -45,15 +48,86 @@ def generate_exegesis(passage):
|
|
| 45 |
except requests.exceptions.RequestException as e:
|
| 46 |
return f"API Error: {e}"
|
| 47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
# Gradio interface
|
| 50 |
demo = gr.Interface(
|
| 51 |
-
fn=generate_exegesis,
|
| 52 |
-
inputs=
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
title="JR Study Bible",
|
| 55 |
-
description="
|
|
|
|
|
|
|
| 56 |
)
|
| 57 |
|
| 58 |
-
if __name__ == "__main__":
|
| 59 |
-
demo.launch()
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
import json
|
| 5 |
+
from cachetools import TTLCache, cached
|
| 6 |
|
| 7 |
# Retrieve the API token from secrets
|
| 8 |
api_token = os.getenv("API_TOKEN")
|
|
|
|
| 10 |
raise ValueError("API token not found. Make sure 'API_TOKEN' is set in the Secrets.")
|
| 11 |
|
| 12 |
# Use the token in your request headers
|
| 13 |
+
API_URL = "https://api-inference.huggingface.co/models/meta-llama/Llama-3.3-70B-Instruct" # Corrected URL
|
| 14 |
HEADERS = {"Authorization": f"Bearer {api_token}"}
|
| 15 |
|
| 16 |
+
# Cache to store API responses for 10 minutes
|
| 17 |
+
cache = TTLCache(maxsize=100, ttl=600)
|
| 18 |
|
| 19 |
+
@cached(cache)
|
| 20 |
def generate_exegesis(passage):
|
| 21 |
if not passage.strip():
|
| 22 |
return "Please enter a Bible passage."
|
|
|
|
| 48 |
except requests.exceptions.RequestException as e:
|
| 49 |
return f"API Error: {e}"
|
| 50 |
|
| 51 |
+
@cached(cache)
|
| 52 |
+
def answer_question(question):
|
| 53 |
+
if not question.strip():
|
| 54 |
+
return "Please enter a question."
|
| 55 |
+
|
| 56 |
+
prompt = f"""<s>[INST] You are a knowledgeable theologian. Answer the following question about the Bible and Christianity:
|
| 57 |
+
|
| 58 |
+
{question} [/INST] Answer:</s>"""
|
| 59 |
+
|
| 60 |
+
payload = {
|
| 61 |
+
"inputs": prompt,
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
try:
|
| 65 |
+
response = requests.post(API_URL, headers=HEADERS, json=payload)
|
| 66 |
+
response.raise_for_status()
|
| 67 |
+
result = response.json()
|
| 68 |
+
print("Full API Response:", json.dumps(result, indent=4)) # Debug output
|
| 69 |
+
|
| 70 |
+
if isinstance(result, list) and len(result) > 0:
|
| 71 |
+
generated_text = result[0].get("generated_text", "")
|
| 72 |
+
marker = "Answer:" # Marker to split on
|
| 73 |
+
if marker in generated_text:
|
| 74 |
+
# Return only the text after the marker
|
| 75 |
+
generated_text = generated_text.split(marker, 1)[1].strip()
|
| 76 |
+
return generated_text or "Error: No response from model."
|
| 77 |
+
else:
|
| 78 |
+
return "Error: Unexpected response format."
|
| 79 |
+
except requests.exceptions.RequestException as e:
|
| 80 |
+
return f"API Error: {e}"
|
| 81 |
+
|
| 82 |
+
@cached(cache)
|
| 83 |
+
def create_sermon(topic):
|
| 84 |
+
if not topic.strip():
|
| 85 |
+
return "Please enter a sermon topic."
|
| 86 |
+
|
| 87 |
+
prompt = f"""<s>[INST] You are a skilled pastor. Create a sermon outline with linked Bible verses for the following topic:
|
| 88 |
+
|
| 89 |
+
{topic} [/INST] Sermon Outline:</s>"""
|
| 90 |
+
|
| 91 |
+
payload = {
|
| 92 |
+
"inputs": prompt,
|
| 93 |
+
}
|
| 94 |
+
|
| 95 |
+
try:
|
| 96 |
+
response = requests.post(API_URL, headers=HEADERS, json=payload)
|
| 97 |
+
response.raise_for_status()
|
| 98 |
+
result = response.json()
|
| 99 |
+
print("Full API Response:", json.dumps(result, indent=4)) # Debug output
|
| 100 |
+
|
| 101 |
+
if isinstance(result, list) and len(result) > 0:
|
| 102 |
+
generated_text = result[0].get("generated_text", "")
|
| 103 |
+
marker = "Sermon Outline:" # Marker to split on
|
| 104 |
+
if marker in generated_text:
|
| 105 |
+
# Return only the text after the marker
|
| 106 |
+
generated_text = generated_text.split(marker, 1)[1].strip()
|
| 107 |
+
return generated_text or "Error: No response from model."
|
| 108 |
+
else:
|
| 109 |
+
return "Error: Unexpected response format."
|
| 110 |
+
except requests.exceptions.RequestException as e:
|
| 111 |
+
return f"API Error: {e}"
|
| 112 |
|
| 113 |
# Gradio interface
|
| 114 |
demo = gr.Interface(
|
| 115 |
+
fn=[generate_exegesis, answer_question, create_sermon],
|
| 116 |
+
inputs=[
|
| 117 |
+
gr.Textbox(label="Enter Bible Passage", placeholder="e.g., John 3:16"),
|
| 118 |
+
gr.Textbox(label="Ask a Question", placeholder="e.g., What is the meaning of life?"),
|
| 119 |
+
gr.Textbox(label="Enter Sermon Topic", placeholder="e.g., Faith")
|
| 120 |
+
],
|
| 121 |
+
outputs=[
|
| 122 |
+
gr.Textbox(label="Exegesis Commentary"),
|
| 123 |
+
gr.Textbox(label="Answer"),
|
| 124 |
+
gr.Textbox(label="Sermon Outline")
|
| 125 |
+
],
|
| 126 |
title="JR Study Bible",
|
| 127 |
+
description="A multifunctional Bible study group app that performs exegesis, answers questions, and helps create sermon outlines.",
|
| 128 |
+
theme="huggingface",
|
| 129 |
+
live=True
|
| 130 |
)
|
| 131 |
|
| 132 |
+
if __name__ == "__main__":
|
| 133 |
+
demo.launch()
|