Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,75 +6,61 @@ import json
|
|
| 6 |
# Retrieve the API token from secrets
|
| 7 |
api_token = os.getenv("API_TOKEN")
|
| 8 |
if not api_token:
|
| 9 |
-
raise ValueError("API token not found.
|
| 10 |
|
|
|
|
| 11 |
API_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.3"
|
| 12 |
HEADERS = {"Authorization": f"Bearer {api_token}"}
|
| 13 |
|
| 14 |
def analyze_figure_of_speech(verse):
|
| 15 |
-
"""Analyze a Bible verse for figures of speech using a language model."""
|
| 16 |
if not verse.strip():
|
| 17 |
-
return "
|
| 18 |
|
| 19 |
-
#
|
| 20 |
-
prompt = f"""
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
- "
|
| 24 |
-
- "
|
| 25 |
-
|
| 26 |
-
Verse: "{verse}"
|
| 27 |
-
[/INST]
|
| 28 |
-
"""
|
| 29 |
|
| 30 |
-
payload = {
|
|
|
|
|
|
|
| 31 |
|
| 32 |
try:
|
| 33 |
-
response = requests.post(API_URL, headers=HEADERS, json=payload
|
| 34 |
response.raise_for_status()
|
| 35 |
result = response.json()
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
return f"Error parsing JSON: {generated_text}"
|
| 50 |
-
|
| 51 |
-
if not isinstance(output_json, list) or len(output_json) == 0:
|
| 52 |
-
return "No figures of speech detected in the verse."
|
| 53 |
-
|
| 54 |
-
return json.dumps(output_json, indent=2, ensure_ascii=False)
|
| 55 |
-
|
| 56 |
except requests.exceptions.RequestException as e:
|
| 57 |
-
return f"
|
| 58 |
-
|
| 59 |
-
# Improved Gradio interface with examples and better formatting
|
| 60 |
-
examples = [
|
| 61 |
-
["I am the vine, you are the branches - John 15:5"],
|
| 62 |
-
["The Lord is my shepherd - Psalm 23:1"],
|
| 63 |
-
["All things are possible with God - Mark 10:27"]
|
| 64 |
-
]
|
| 65 |
|
| 66 |
demo = gr.Interface(
|
| 67 |
fn=analyze_figure_of_speech,
|
| 68 |
inputs=gr.Textbox(
|
| 69 |
label="Enter Bible Verse",
|
| 70 |
-
placeholder="e.g., 'I am the vine
|
| 71 |
lines=3
|
| 72 |
),
|
| 73 |
-
outputs=gr.
|
| 74 |
-
title="JR-Sacred Syntax
|
| 75 |
-
description="
|
| 76 |
-
examples=examples
|
| 77 |
)
|
| 78 |
|
| 79 |
if __name__ == "__main__":
|
| 80 |
-
demo.launch()
|
|
|
|
| 6 |
# Retrieve the API token from secrets
|
| 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 the token in your request headers
|
| 12 |
API_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.3"
|
| 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 to analyze figures of speech in the verse.
|
| 20 |
+
prompt = f"""<s>[INST] You are JR-Sacred Syntax, an expert biblical scholar specializing in the analysis of Bible verses.
|
| 21 |
+
Analyze the following Bible verse to identify any figures of speech (e.g., Metaphor, Synecdoche, Hyperbole, Simile, Paradox).
|
| 22 |
+
Return your answer as a JSON array where each element contains:
|
| 23 |
+
- "figure": the type of figure of speech,
|
| 24 |
+
- "phrase": the exact phrase in the verse,
|
| 25 |
+
- "explanation": a brief explanation of why it qualifies as that figure in a biblical context.
|
| 26 |
+
Bible Verse: "{verse}"
|
| 27 |
+
[/INST] Figures:</s>"""
|
|
|
|
| 28 |
|
| 29 |
+
payload = {
|
| 30 |
+
"inputs": prompt,
|
| 31 |
+
}
|
| 32 |
|
| 33 |
try:
|
| 34 |
+
response = requests.post(API_URL, headers=HEADERS, json=payload)
|
| 35 |
response.raise_for_status()
|
| 36 |
result = response.json()
|
| 37 |
|
| 38 |
+
if isinstance(result, list) and len(result) > 0:
|
| 39 |
+
generated_text = result[0].get("generated_text", "")
|
| 40 |
+
marker = "Figures:" # Marker to help extract the output
|
| 41 |
+
if marker in generated_text:
|
| 42 |
+
generated_text = generated_text.split(marker, 1)[1].strip()
|
| 43 |
+
try:
|
| 44 |
+
output_json = json.loads(generated_text)
|
| 45 |
+
return json.dumps(output_json, indent=2)
|
| 46 |
+
except json.JSONDecodeError:
|
| 47 |
+
return generated_text
|
| 48 |
+
else:
|
| 49 |
+
return "Error: Unexpected response format."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
except requests.exceptions.RequestException as e:
|
| 51 |
+
return f"API Error: {e}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
demo = gr.Interface(
|
| 54 |
fn=analyze_figure_of_speech,
|
| 55 |
inputs=gr.Textbox(
|
| 56 |
label="Enter Bible Verse",
|
| 57 |
+
placeholder="e.g., 'I am the vine, you are the branches' - John 15:5",
|
| 58 |
lines=3
|
| 59 |
),
|
| 60 |
+
outputs=gr.Textbox(label="Figures of Speech Analysis (JSON)"),
|
| 61 |
+
title="JR-Sacred Syntax: Bible Figures of Speech Detector",
|
| 62 |
+
description="Enter a Bible verse to detect and analyze figures of speech with detailed biblical explanations."
|
|
|
|
| 63 |
)
|
| 64 |
|
| 65 |
if __name__ == "__main__":
|
| 66 |
+
demo.launch()
|