Spaces:
Build error
Build error
| 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() | |