Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,51 +3,48 @@ import gradio as gr
|
|
| 3 |
import requests
|
| 4 |
import json
|
| 5 |
|
| 6 |
-
# Retrieve the API token from
|
| 7 |
api_token = os.getenv("API_TOKEN")
|
| 8 |
if not api_token:
|
| 9 |
raise ValueError("API token not found. Make sure 'API_TOKEN' is set in the Secrets.")
|
| 10 |
|
| 11 |
-
# Use
|
| 12 |
-
API_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.
|
| 13 |
HEADERS = {"Authorization": f"Bearer {api_token}"}
|
| 14 |
|
| 15 |
def analyze_figure_of_speech(verse):
|
| 16 |
if not verse.strip():
|
| 17 |
-
|
| 18 |
|
| 19 |
-
# Construct a prompt to
|
| 20 |
-
prompt = f"""
|
| 21 |
-
|
|
|
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
{
|
| 26 |
"verse": "[Provide the NKJV Bible verse]",
|
| 27 |
"figure": "[Identify the figure of speech]",
|
| 28 |
-
"explanation": "[
|
| 29 |
"examples": [
|
| 30 |
-
{
|
| 31 |
"phrase": "[Identify the phrase from the verse]",
|
| 32 |
"analysis": "[Explain why this phrase qualifies as the identified figure of speech]"
|
| 33 |
-
}
|
| 34 |
]
|
| 35 |
-
}
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
Example:
|
| 40 |
-
{
|
| 41 |
"verse": "Acts 5:9 - 'The feet of those who have buried your husband are at the door…'",
|
| 42 |
"figure": "Synecdoche",
|
| 43 |
"explanation": "'Feet' here stands for the entire persons (the men) who buried her husband. It focuses on one part (their feet) to represent the whole person.",
|
| 44 |
"examples": [
|
| 45 |
-
{
|
| 46 |
"phrase": "feet",
|
| 47 |
"analysis": "The term 'feet' is used to represent the whole person."
|
| 48 |
-
}
|
| 49 |
]
|
| 50 |
-
}
|
| 51 |
|
| 52 |
Now, analyze the following Bible verse:
|
| 53 |
"{verse}"
|
|
@@ -55,27 +52,26 @@ Now, analyze the following Bible verse:
|
|
| 55 |
|
| 56 |
payload = {
|
| 57 |
"inputs": prompt,
|
|
|
|
| 58 |
}
|
| 59 |
|
| 60 |
try:
|
|
|
|
| 61 |
response = requests.post(API_URL, headers=HEADERS, json=payload)
|
| 62 |
response.raise_for_status()
|
| 63 |
result = response.json()
|
| 64 |
|
| 65 |
if isinstance(result, list) and len(result) > 0:
|
| 66 |
generated_text = result[0].get("generated_text", "")
|
| 67 |
-
marker = "Figures:" # Marker to help extract the output
|
| 68 |
-
if marker in generated_text:
|
| 69 |
-
generated_text = generated_text.split(marker, 1)[1].strip()
|
| 70 |
try:
|
| 71 |
output_json = json.loads(generated_text)
|
| 72 |
-
|
| 73 |
except json.JSONDecodeError:
|
| 74 |
-
|
| 75 |
else:
|
| 76 |
-
|
| 77 |
except requests.exceptions.RequestException as e:
|
| 78 |
-
|
| 79 |
|
| 80 |
demo = gr.Interface(
|
| 81 |
fn=analyze_figure_of_speech,
|
|
@@ -86,7 +82,8 @@ demo = gr.Interface(
|
|
| 86 |
),
|
| 87 |
outputs=gr.Textbox(label="Figures of Speech Analysis (JSON)"),
|
| 88 |
title="JR-Sacred Syntax: Bible Figures of Speech Detector",
|
| 89 |
-
description="Enter a Bible verse to detect and analyze figures of speech with detailed biblical explanations."
|
|
|
|
| 90 |
)
|
| 91 |
|
| 92 |
if __name__ == "__main__":
|
|
|
|
| 3 |
import requests
|
| 4 |
import json
|
| 5 |
|
| 6 |
+
# Retrieve the API token from environment variables
|
| 7 |
api_token = os.getenv("API_TOKEN")
|
| 8 |
if not api_token:
|
| 9 |
raise ValueError("API token not found. Make sure 'API_TOKEN' is set in the Secrets.")
|
| 10 |
|
| 11 |
+
# Use a faster and optimized model
|
| 12 |
+
API_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.2"
|
| 13 |
HEADERS = {"Authorization": f"Bearer {api_token}"}
|
| 14 |
|
| 15 |
def analyze_figure_of_speech(verse):
|
| 16 |
if not verse.strip():
|
| 17 |
+
yield "Please enter a Bible verse."
|
| 18 |
|
| 19 |
+
# Construct a refined prompt to ensure structured JSON output
|
| 20 |
+
prompt = f"""<s>[INST] You are JR-Sacred Syntax, an expert biblical scholar specializing in figures of speech in the Bible.
|
| 21 |
+
Analyze the given verse and determine if it contains a figure of speech such as Synecdoche, Metonymy, Hyperbole, Simile, or Paradox.
|
| 22 |
+
Follow this structured JSON format:
|
| 23 |
|
| 24 |
+
{{
|
|
|
|
|
|
|
| 25 |
"verse": "[Provide the NKJV Bible verse]",
|
| 26 |
"figure": "[Identify the figure of speech]",
|
| 27 |
+
"explanation": "[Clear explanation similar to the reference examples]",
|
| 28 |
"examples": [
|
| 29 |
+
{{
|
| 30 |
"phrase": "[Identify the phrase from the verse]",
|
| 31 |
"analysis": "[Explain why this phrase qualifies as the identified figure of speech]"
|
| 32 |
+
}}
|
| 33 |
]
|
| 34 |
+
}}
|
| 35 |
|
| 36 |
+
Example Output:
|
| 37 |
+
{{
|
|
|
|
|
|
|
| 38 |
"verse": "Acts 5:9 - 'The feet of those who have buried your husband are at the door…'",
|
| 39 |
"figure": "Synecdoche",
|
| 40 |
"explanation": "'Feet' here stands for the entire persons (the men) who buried her husband. It focuses on one part (their feet) to represent the whole person.",
|
| 41 |
"examples": [
|
| 42 |
+
{{
|
| 43 |
"phrase": "feet",
|
| 44 |
"analysis": "The term 'feet' is used to represent the whole person."
|
| 45 |
+
}}
|
| 46 |
]
|
| 47 |
+
}}
|
| 48 |
|
| 49 |
Now, analyze the following Bible verse:
|
| 50 |
"{verse}"
|
|
|
|
| 52 |
|
| 53 |
payload = {
|
| 54 |
"inputs": prompt,
|
| 55 |
+
"parameters": {"max_new_tokens": 300, "temperature": 0.7, "top_p": 0.9},
|
| 56 |
}
|
| 57 |
|
| 58 |
try:
|
| 59 |
+
yield "Processing... Please wait."
|
| 60 |
response = requests.post(API_URL, headers=HEADERS, json=payload)
|
| 61 |
response.raise_for_status()
|
| 62 |
result = response.json()
|
| 63 |
|
| 64 |
if isinstance(result, list) and len(result) > 0:
|
| 65 |
generated_text = result[0].get("generated_text", "")
|
|
|
|
|
|
|
|
|
|
| 66 |
try:
|
| 67 |
output_json = json.loads(generated_text)
|
| 68 |
+
yield json.dumps(output_json, indent=2)
|
| 69 |
except json.JSONDecodeError:
|
| 70 |
+
yield generated_text
|
| 71 |
else:
|
| 72 |
+
yield "Error: Unexpected response format."
|
| 73 |
except requests.exceptions.RequestException as e:
|
| 74 |
+
yield f"API Error: {e}"
|
| 75 |
|
| 76 |
demo = gr.Interface(
|
| 77 |
fn=analyze_figure_of_speech,
|
|
|
|
| 82 |
),
|
| 83 |
outputs=gr.Textbox(label="Figures of Speech Analysis (JSON)"),
|
| 84 |
title="JR-Sacred Syntax: Bible Figures of Speech Detector",
|
| 85 |
+
description="Enter a Bible verse to detect and analyze figures of speech with detailed biblical explanations.",
|
| 86 |
+
live=True # Enables real-time updates
|
| 87 |
)
|
| 88 |
|
| 89 |
if __name__ == "__main__":
|