Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,73 +6,55 @@ import json
|
|
| 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
|
| 10 |
|
| 11 |
-
#
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
"
|
| 44 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
}}
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
Now, analyze the following Bible verse:
|
| 50 |
-
"{verse}"
|
| 51 |
-
[/INST]</s>"""
|
| 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
|
| 65 |
generated_text = result[0].get("generated_text", "")
|
| 66 |
try:
|
| 67 |
output_json = json.loads(generated_text)
|
| 68 |
-
|
| 69 |
except json.JSONDecodeError:
|
| 70 |
-
|
| 71 |
else:
|
| 72 |
-
|
| 73 |
except requests.exceptions.RequestException as e:
|
| 74 |
-
|
| 75 |
|
|
|
|
| 76 |
demo = gr.Interface(
|
| 77 |
fn=analyze_figure_of_speech,
|
| 78 |
inputs=gr.Textbox(
|
|
@@ -82,9 +64,9 @@ demo = gr.Interface(
|
|
| 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
|
| 86 |
-
live=
|
| 87 |
)
|
| 88 |
|
| 89 |
if __name__ == "__main__":
|
| 90 |
-
demo.launch()
|
|
|
|
| 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 environment variables.")
|
| 10 |
|
| 11 |
+
# API configuration
|
| 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 |
+
return "Please enter a Bible verse."
|
| 18 |
+
|
| 19 |
+
# Construct a prompt to instruct the model
|
| 20 |
+
prompt = f"""
|
| 21 |
+
[INST] You are JR-Sacred Syntax, an expert biblical scholar specializing in Bible figures of speech.
|
| 22 |
+
Identify the literary device in the following verse (e.g., Synecdoche, Metonymy, Hyperbole, Simile, Paradox) and structure the response as JSON:
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
{{
|
| 25 |
+
"verse": "{verse}",
|
| 26 |
+
"figure": "[Identify the figure of speech]",
|
| 27 |
+
"explanation": "[Give a detailed yet concise explanation]",
|
| 28 |
+
"examples": [
|
| 29 |
+
{{
|
| 30 |
+
"phrase": "[Highlight the specific phrase]",
|
| 31 |
+
"analysis": "[Explain why this phrase fits the identified figure of speech]"
|
| 32 |
+
}}
|
| 33 |
+
]
|
| 34 |
}}
|
| 35 |
+
[/INST]
|
| 36 |
+
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
+
payload = {"inputs": prompt, "parameters": {"max_new_tokens": 250, "temperature": 0.7, "top_p": 0.9}}
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
try:
|
|
|
|
| 41 |
response = requests.post(API_URL, headers=HEADERS, json=payload)
|
| 42 |
response.raise_for_status()
|
| 43 |
result = response.json()
|
| 44 |
|
| 45 |
+
if isinstance(result, list) and result:
|
| 46 |
generated_text = result[0].get("generated_text", "")
|
| 47 |
try:
|
| 48 |
output_json = json.loads(generated_text)
|
| 49 |
+
return json.dumps(output_json, indent=2)
|
| 50 |
except json.JSONDecodeError:
|
| 51 |
+
return "Error processing response. Please try again."
|
| 52 |
else:
|
| 53 |
+
return "Unexpected response format."
|
| 54 |
except requests.exceptions.RequestException as e:
|
| 55 |
+
return f"API Error: {e}"
|
| 56 |
|
| 57 |
+
# Gradio Interface
|
| 58 |
demo = gr.Interface(
|
| 59 |
fn=analyze_figure_of_speech,
|
| 60 |
inputs=gr.Textbox(
|
|
|
|
| 64 |
),
|
| 65 |
outputs=gr.Textbox(label="Figures of Speech Analysis (JSON)"),
|
| 66 |
title="JR-Sacred Syntax: Bible Figures of Speech Detector",
|
| 67 |
+
description="Enter a Bible verse to detect and analyze figures of speech with detailed explanations.",
|
| 68 |
+
live=False # Prevents automatic execution
|
| 69 |
)
|
| 70 |
|
| 71 |
if __name__ == "__main__":
|
| 72 |
+
demo.launch()
|