File size: 4,305 Bytes
823834e
aa94de3
823834e
a576072
aa94de3
eea66cf
5a55b83
ee64d73
0f483b0
eea66cf
 
 
25d3696
 
eea66cf
25d3696
 
 
eea66cf
 
ee64d73
eea66cf
 
ee64d73
39d5100
eea66cf
 
 
 
 
 
ee64d73
 
 
eea66cf
 
 
 
 
0f483b0
 
eea66cf
0f483b0
 
ee64d73
eea66cf
 
 
3a33043
eea66cf
 
 
 
 
 
 
 
25d3696
fbc8dab
eea66cf
1488a70
823834e
eea66cf
823834e
fbc8dab
5b14ccc
 
eea66cf
 
5b14ccc
 
eea66cf
1488a70
5b14ccc
eea66cf
 
209b85c
eea66cf
d0f52f7
eea66cf
d0f52f7
 
06a139b
209b85c
 
790978b
eea66cf
1488a70
5b14ccc
1488a70
5b14ccc
eea66cf
a576072
39d5100
7d42faa
eea66cf
a576072
 
5b14ccc
eea66cf
ee64d73
 
5b14ccc
790978b
1488a70
eea66cf
aa94de3
 
 
eea66cf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import os
import gradio as gr
import requests
import json

# Constants
API_URL = "https://api-inference.huggingface.co/models/deepseek-ai/DeepSeek-R1"
MAX_NEW_TOKENS = 1000  # Increased to handle longer inputs
TEMPERATURE = 0.7
TOP_P = 0.9

# Retrieve the API token from environment variables
api_token = os.getenv("API_TOKEN")
if not api_token:
    raise ValueError("API token not found. Make sure 'API_TOKEN' is set in the environment variables.")

HEADERS = {"Authorization": f"Bearer {api_token}"}

def analyze_figure_of_speech(text: str) -> str:
    """
    Analyze the figures of speech in a given Bible passage using a Hugging Face model.
    
    Args:
        text (str): The Bible passage to analyze.
    
    Returns:
        str: Analysis result formatted as text or error message.
    """
    if not text.strip():
        return "Please enter a Bible verse or passage."
    
    # Split the input into individual verses
    verses = text.splitlines()
    
    # Construct the prompt for the model
    prompt = (
        f"""<s>[INST] You are JR-Sacred Syntax, an expert biblical scholar specializing in Bible analysis.
        Analyze the following Bible passage to identify any figures of speech (e.g., Metaphor, Synecdoche, Hyperbole, Simile, Paradox, and others).
        Return your answer as a JSON array where each element includes:
        - "verses": Display the Bible verse(s) in NJV with the verses number,
        - "figure": identify the type of figure of speech(s) in each of the verses,
        - "definition": define the figure of speech you identified,
        - "phrase": Give the exact phrase from the verse,
        - "explanation": Give a brief explanation in the biblical context.
        Consider each verse individually and provide a detailed analysis.
        Bible Passage: "{text}"
        [/INST] Figures: </s>"""
    )

    payload = {
        "inputs": prompt,
        "parameters": {
            "max_new_tokens": MAX_NEW_TOKENS,
            "temperature": TEMPERATURE,
            "top_p": TOP_P
        }
    }
    
    try:
        # Send request to Hugging Face API
        response = requests.post(API_URL, headers=HEADERS, json=payload)
        response.raise_for_status()
        
        result = response.json()
        
        if isinstance(result, list) and len(result) > 0:
            generated_text = result[0].get("generated_text", "")
            marker = "Figures:"
            
            if marker in generated_text:
                generated_text = generated_text.split(marker, 1)[1].strip()
            
            try:
                output_json = json.loads(generated_text)
                if isinstance(output_json, list):
                    # Format output for readability
                    formatted_output = "\n\n".join(
                        f"Verse(s): {item.get('verses', '')}\n"
                        f"Figure: {item.get('figure', '')}\n"
                        f"Definition: {item.get('definition', '')}\n"
                        f'Phrase: "{item.get("phrase", "")}"\n'
                        f"Explanation: {item.get('explanation', '')}"
                        for item in output_json
                    )
                    return formatted_output
                else:
                    return json.dumps(output_json, indent=2)
            except json.JSONDecodeError:
                return generated_text
        else:
            return "Error: Unexpected response format."
    
    except requests.exceptions.RequestException as e:
        return f"API Error: {e}"

# Gradio Interface setup
demo = gr.Interface(
    fn=analyze_figure_of_speech,
    inputs=gr.Textbox(
        label="Enter Bible Verse(s) or Passage",
        placeholder="e.g., 'In the beginning God created the heavens and the earth.' - Gen 1:1\n'And the earth was without form and void; and darkness was upon the face of the deep.' - Gen 1:2\n'And the Spirit of God moved upon the face of the waters.' - Gen 1:3",
        lines=10
    ),
    outputs=gr.Textbox(label="Figures of Speech Analysis"),
    title="JR-Sacred Syntax: Bible Figures of Speech Detector",
    description="Enter a Bible verse or passage to detect and analyze figures of speech with detailed biblical explanations."
)

if __name__ == "__main__":
    demo.launch()