Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,74 +3,104 @@ import gradio as gr
|
|
| 3 |
import requests
|
| 4 |
import json
|
| 5 |
|
| 6 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
# 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(
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
-
payload = {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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:"
|
|
|
|
| 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 |
-
if isinstance(output_json, list):
|
| 46 |
-
#
|
| 47 |
formatted_output = "\n\n".join(
|
|
|
|
| 48 |
f"Figure: {item.get('figure', '')}\n"
|
|
|
|
| 49 |
f'Phrase: "{item.get("phrase", "")}"\n'
|
| 50 |
f"Explanation: {item.get('explanation', '')}"
|
| 51 |
for item in output_json
|
| 52 |
)
|
| 53 |
return formatted_output
|
| 54 |
else:
|
| 55 |
-
return json.dumps(output_json, indent=2)
|
| 56 |
except json.JSONDecodeError:
|
| 57 |
return generated_text
|
| 58 |
else:
|
| 59 |
return "Error: Unexpected response format."
|
|
|
|
| 60 |
except requests.exceptions.RequestException as e:
|
| 61 |
return f"API Error: {e}"
|
| 62 |
|
|
|
|
| 63 |
demo = gr.Interface(
|
| 64 |
fn=analyze_figure_of_speech,
|
| 65 |
inputs=gr.Textbox(
|
| 66 |
-
label="Enter Bible Verse",
|
| 67 |
-
placeholder="e.g., 'I am the vine, you are the branches' - John 15:5",
|
| 68 |
-
lines=
|
| 69 |
),
|
| 70 |
outputs=gr.Textbox(label="Figures of Speech Analysis"),
|
| 71 |
title="JR-Sacred Syntax: Bible Figures of Speech Detector",
|
| 72 |
-
description="Enter a Bible verse to detect and analyze figures of speech with detailed biblical explanations."
|
| 73 |
)
|
| 74 |
|
| 75 |
if __name__ == "__main__":
|
| 76 |
-
demo.launch()
|
|
|
|
| 3 |
import requests
|
| 4 |
import json
|
| 5 |
|
| 6 |
+
# Constants
|
| 7 |
+
API_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.3"
|
| 8 |
+
MAX_NEW_TOKENS = 500 # Increased to handle longer inputs
|
| 9 |
+
TEMPERATURE = 0.5
|
| 10 |
+
TOP_P = 0.9
|
| 11 |
+
|
| 12 |
+
# Retrieve the API token from environment variables
|
| 13 |
api_token = os.getenv("API_TOKEN")
|
| 14 |
if not api_token:
|
| 15 |
+
raise ValueError("API token not found. Make sure 'API_TOKEN' is set in the environment variables.")
|
| 16 |
|
|
|
|
|
|
|
| 17 |
HEADERS = {"Authorization": f"Bearer {api_token}"}
|
| 18 |
|
| 19 |
+
def analyze_figure_of_speech(text: str) -> str:
|
| 20 |
+
"""
|
| 21 |
+
Analyze the figures of speech in a given Bible text using a Hugging Face model.
|
| 22 |
+
|
| 23 |
+
Args:
|
| 24 |
+
text (str): The Bible text to analyze.
|
| 25 |
|
| 26 |
+
Returns:
|
| 27 |
+
str: Analysis result formatted as text or error message.
|
| 28 |
+
"""
|
| 29 |
+
if not text.strip():
|
| 30 |
+
return "Please enter a Bible verse or passage."
|
| 31 |
+
|
| 32 |
+
# Construct the prompt for the model
|
| 33 |
+
prompt = (
|
| 34 |
+
f"""<s>[INST] You are JR-Sacred Syntax, an expert biblical scholar specializing in Bible analysis.
|
| 35 |
+
Analyze the following Bible passage to identify any figures of speech (e.g., Metaphor, Synecdoche, Hyperbole, Simile, Paradox, and others).
|
| 36 |
+
Return your answer as a JSON array where each element includes:
|
| 37 |
+
- "verses": the Bible verse(s) in NJV,
|
| 38 |
+
- "figure": the type of figure of speech,
|
| 39 |
+
- "definition": define the figure of speech you identified,
|
| 40 |
+
- "phrase": the exact phrase from the verse,
|
| 41 |
+
- "explanation": a brief explanation in the biblical context.
|
| 42 |
+
Bible Passage: "{text}"
|
| 43 |
+
[/INST] Figures: </s>"""
|
| 44 |
+
)
|
| 45 |
|
| 46 |
+
payload = {
|
| 47 |
+
"inputs": prompt,
|
| 48 |
+
"parameters": {
|
| 49 |
+
"max_new_tokens": MAX_NEW_TOKENS,
|
| 50 |
+
"temperature": TEMPERATURE,
|
| 51 |
+
"top_p": TOP_P
|
| 52 |
+
}
|
| 53 |
+
}
|
| 54 |
|
| 55 |
try:
|
| 56 |
+
# Send request to Hugging Face API
|
| 57 |
response = requests.post(API_URL, headers=HEADERS, json=payload)
|
| 58 |
response.raise_for_status()
|
| 59 |
+
|
| 60 |
result = response.json()
|
| 61 |
|
| 62 |
if isinstance(result, list) and len(result) > 0:
|
| 63 |
generated_text = result[0].get("generated_text", "")
|
| 64 |
+
marker = "Figures:"
|
| 65 |
+
|
| 66 |
if marker in generated_text:
|
| 67 |
generated_text = generated_text.split(marker, 1)[1].strip()
|
| 68 |
+
|
| 69 |
try:
|
| 70 |
output_json = json.loads(generated_text)
|
| 71 |
+
if isinstance(output_json, list):
|
| 72 |
+
# Format output for readability
|
| 73 |
formatted_output = "\n\n".join(
|
| 74 |
+
f"Verse(s): {item.get('verses', '')}\n"
|
| 75 |
f"Figure: {item.get('figure', '')}\n"
|
| 76 |
+
f"Definition: {item.get('definition', '')}\n"
|
| 77 |
f'Phrase: "{item.get("phrase", "")}"\n'
|
| 78 |
f"Explanation: {item.get('explanation', '')}"
|
| 79 |
for item in output_json
|
| 80 |
)
|
| 81 |
return formatted_output
|
| 82 |
else:
|
| 83 |
+
return json.dumps(output_json, indent=2)
|
| 84 |
except json.JSONDecodeError:
|
| 85 |
return generated_text
|
| 86 |
else:
|
| 87 |
return "Error: Unexpected response format."
|
| 88 |
+
|
| 89 |
except requests.exceptions.RequestException as e:
|
| 90 |
return f"API Error: {e}"
|
| 91 |
|
| 92 |
+
# Gradio Interface setup
|
| 93 |
demo = gr.Interface(
|
| 94 |
fn=analyze_figure_of_speech,
|
| 95 |
inputs=gr.Textbox(
|
| 96 |
+
label="Enter Bible Verse(s) or Passage",
|
| 97 |
+
placeholder="e.g., 'I am the vine, you are the branches' - John 15:5 or multiple verses separated by newline",
|
| 98 |
+
lines=10 # Increased to allow input of multiple verses
|
| 99 |
),
|
| 100 |
outputs=gr.Textbox(label="Figures of Speech Analysis"),
|
| 101 |
title="JR-Sacred Syntax: Bible Figures of Speech Detector",
|
| 102 |
+
description="Enter a Bible verse or passage to detect and analyze figures of speech with detailed biblical explanations."
|
| 103 |
)
|
| 104 |
|
| 105 |
if __name__ == "__main__":
|
| 106 |
+
demo.launch()
|