Spaces:
Build error
Build error
Update App.py
Browse files
App.py
CHANGED
|
@@ -1,72 +1,61 @@
|
|
| 1 |
import json
|
| 2 |
import gradio as gr
|
| 3 |
-
from
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
|
|
|
| 7 |
|
| 8 |
-
def
|
| 9 |
"""
|
| 10 |
-
|
| 11 |
|
| 12 |
-
The
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
-
|
| 16 |
-
- "phrase": the phrase in the verse,
|
| 17 |
-
- "explanation": explanation in biblical context.
|
| 18 |
-
"""
|
| 19 |
-
# Build the message list starting with the system message.
|
| 20 |
-
messages = [{"role": "system", "content": system_message}]
|
| 21 |
-
|
| 22 |
-
# Append previous conversation history.
|
| 23 |
-
for user_msg, assistant_msg in history:
|
| 24 |
-
if user_msg:
|
| 25 |
-
messages.append({"role": "user", "content": user_msg})
|
| 26 |
-
if assistant_msg:
|
| 27 |
-
messages.append({"role": "assistant", "content": assistant_msg})
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
temperature=temperature,
|
| 39 |
-
top_p=top_p,
|
| 40 |
-
):
|
| 41 |
-
token = chat_message.choices[0].delta.content
|
| 42 |
-
response += token
|
| 43 |
-
yield response
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
-
#
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (Nucleus Sampling)"),
|
| 62 |
-
],
|
| 63 |
-
title="JR‑Sacred Syntax: Bible Figures of Speech Detector",
|
| 64 |
-
description=(
|
| 65 |
-
"Enter a Bible verse to have it analyzed for figures of speech. "
|
| 66 |
-
"JR‑Sacred Syntax will detect and explain literary devices in the verse, "
|
| 67 |
-
"returning the results in a structured JSON format."
|
| 68 |
-
)
|
| 69 |
)
|
| 70 |
|
| 71 |
if __name__ == "__main__":
|
| 72 |
-
|
|
|
|
| 1 |
import json
|
| 2 |
import gradio as gr
|
| 3 |
+
from transformers import pipeline
|
| 4 |
|
| 5 |
+
# Load the text generation model (can swap for a Bible-specific LLM)
|
| 6 |
+
model_name = "google/flan-t5-large" # Change if needed
|
| 7 |
+
generator = pipeline("text2text-generation", model=model_name, trust_remote_code=True)
|
| 8 |
|
| 9 |
+
def analyze_bible_figure_of_speech(verse):
|
| 10 |
"""
|
| 11 |
+
Analyzes a Bible verse for figures of speech.
|
| 12 |
|
| 13 |
+
The function constructs a structured prompt for an LLM, requesting it to:
|
| 14 |
+
- Identify any figure of speech in the verse (e.g., Metaphor, Hyperbole, Synecdoche, Paradox, Simile).
|
| 15 |
+
- Extract the phrase demonstrating the figure of speech.
|
| 16 |
+
- Explain why it's a figure of speech in biblical context.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
Expected Output:
|
| 19 |
+
A structured JSON array with:
|
| 20 |
+
- "figure": Type of figure of speech.
|
| 21 |
+
- "phrase": The phrase in the verse.
|
| 22 |
+
- "explanation": Explanation in biblical context.
|
| 23 |
+
"""
|
| 24 |
+
if not verse.strip():
|
| 25 |
+
return "Please enter a Bible verse."
|
| 26 |
|
| 27 |
+
prompt = f"""
|
| 28 |
+
Analyze the following Bible verse and identify any figures of speech present (e.g., Metaphor, Hyperbole, Synecdoche, Paradox, Simile).
|
| 29 |
+
Provide a JSON response with:
|
| 30 |
+
- "figure": Type of figure of speech.
|
| 31 |
+
- "phrase": The phrase in the verse.
|
| 32 |
+
- "explanation": Why it is a figure of speech in biblical context.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
+
Bible Verse: \"{verse}\"
|
| 35 |
+
"""
|
| 36 |
+
|
| 37 |
+
try:
|
| 38 |
+
# Generate the output from the LLM
|
| 39 |
+
result = generator(prompt, max_length=512, num_return_sequences=1)
|
| 40 |
+
generated_text = result[0]['generated_text']
|
| 41 |
+
|
| 42 |
+
# Try parsing the output as JSON
|
| 43 |
+
try:
|
| 44 |
+
output_json = json.loads(generated_text)
|
| 45 |
+
return json.dumps(output_json, indent=2) # Pretty-print JSON
|
| 46 |
+
except json.JSONDecodeError:
|
| 47 |
+
return generated_text # Return raw text if JSON parsing fails
|
| 48 |
+
except Exception as e:
|
| 49 |
+
return f"Error: {str(e)}"
|
| 50 |
|
| 51 |
+
# Gradio UI
|
| 52 |
+
interface = gr.Interface(
|
| 53 |
+
fn=analyze_bible_figure_of_speech,
|
| 54 |
+
inputs=gr.Textbox(label="Enter a Bible Verse", placeholder="E.g., 'I am the vine, you are the branches' - John 15:5", lines=3),
|
| 55 |
+
outputs=gr.Textbox(label="Figures of Speech Analysis (JSON)"),
|
| 56 |
+
title="Bible Figures of Speech Detector",
|
| 57 |
+
description="Enter a Bible verse to analyze its figures of speech, such as metaphor, simile, or hyperbole. The app will identify the figure, extract the phrase, and provide an explanation."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
)
|
| 59 |
|
| 60 |
if __name__ == "__main__":
|
| 61 |
+
interface.launch()
|