Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,51 +1,52 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import gradio as gr
|
| 3 |
-
import requests
|
| 4 |
-
|
| 5 |
-
# Retrieve the API token from environment variables
|
| 6 |
-
|
| 7 |
-
if not
|
| 8 |
-
raise ValueError("API token not found. Please set the API_TOKEN environment variable.")
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
"
|
| 23 |
-
|
| 24 |
-
"
|
| 25 |
-
"
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
response
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import requests
|
| 4 |
+
|
| 5 |
+
# Retrieve the API token from environment variables
|
| 6 |
+
api_token = os.getenv("API_TOKEN")
|
| 7 |
+
if not api_token:
|
| 8 |
+
raise ValueError("API token not found. Please set the API_TOKEN environment variable.")
|
| 9 |
+
|
| 10 |
+
# Hugging Face Inference API Details
|
| 11 |
+
API_URL = "https://api-inference.huggingface.co/models/meta-llama/Llama-3.2-1B-Instruct"
|
| 12 |
+
HEADERS = {"Authorization": f"Bearer {api_token}"}
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def generate_exegesis(passage):
|
| 16 |
+
if not passage.strip():
|
| 17 |
+
return "Please enter a Bible passage."
|
| 18 |
+
|
| 19 |
+
prompt = f"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 with meanings. Historical and cultural context. Theological significance. for:\n\n{passage}\n\nExegesis:"
|
| 20 |
+
|
| 21 |
+
payload = {
|
| 22 |
+
"inputs": prompt,
|
| 23 |
+
"parameters": {
|
| 24 |
+
"max_length": 250,
|
| 25 |
+
"temperature": 0.7,
|
| 26 |
+
"do_sample": True
|
| 27 |
+
}
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
try:
|
| 31 |
+
response = requests.post(API_URL, headers=HEADERS, json=payload)
|
| 32 |
+
response.raise_for_status() # Raise an error for bad responses (4xx, 5xx)
|
| 33 |
+
result = response.json()
|
| 34 |
+
|
| 35 |
+
if isinstance(result, list) and len(result) > 0:
|
| 36 |
+
return result[0].get("generated_text", "Error: No response from model.")
|
| 37 |
+
else:
|
| 38 |
+
return "Error: Unexpected response format."
|
| 39 |
+
except requests.exceptions.RequestException as e:
|
| 40 |
+
return f"API Error: {e}"
|
| 41 |
+
|
| 42 |
+
# Gradio interface
|
| 43 |
+
demo = gr.Interface(
|
| 44 |
+
fn=generate_exegesis,
|
| 45 |
+
inputs=gr.Textbox(label="Enter Bible Passage", placeholder="e.g., John 3:16"),
|
| 46 |
+
outputs=gr.Textbox(label="Exegesis Commentary"),
|
| 47 |
+
title="JR Study Bible",
|
| 48 |
+
description="Enter a Bible passage to receive insightful exegesis commentary using the Hugging Face API."
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
if __name__ == "__main__":
|
| 52 |
+
demo.launch()
|