Spaces:
Build error
Build error
| import os | |
| import gradio as gr | |
| import requests | |
| import json | |
| # Retrieve the API token from secrets | |
| api_token = os.getenv("API_TOKEN") | |
| if not api_token: | |
| raise ValueError("API token not found. Make sure 'API_TOKEN' is set in the Secrets.") | |
| # Use the token in your request headers | |
| API_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.3" | |
| HEADERS = {"Authorization": f"Bearer {api_token}"} | |
| def analyze_figure_of_speech(verse): | |
| if not verse.strip(): | |
| return "Please enter a Bible verse." | |
| # Construct a prompt to instruct the model to analyze figures of speech in the verse. | |
| prompt = f""" | |
| [INST] You are JR-Sacred Syntax, an expert biblical scholar specializing in Bible figures of speech. | |
| Identify the literary device in the following verse (e.g., Synecdoche, Metonymy, Hyperbole, Simile, Paradox) and structure the response as JSON: | |
| {{ | |
| "verse": "{verse}", | |
| "figure": "[Identify the figure of speech]", | |
| "explanation": "[Give a detailed yet concise explanation]", | |
| "examples": [ | |
| {{ | |
| "phrase": "[Highlight the specific phrase]", | |
| "analysis": "[Explain why this phrase fits the identified figure of speech]" | |
| }} | |
| ] | |
| }} | |
| [/INST] | |
| """ | |
| payload = {"inputs": prompt, "parameters": {"max_new_tokens": 250, "temperature": 0.7, "top_p": 0.9}} | |
| try: | |
| 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:" # Marker to help extract the output | |
| if marker in generated_text: | |
| generated_text = generated_text.split(marker, 1)[1].strip() | |
| try: | |
| output_json = json.loads(generated_text) | |
| 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}" | |
| demo = gr.Interface( | |
| fn=analyze_figure_of_speech, | |
| inputs=gr.Textbox( | |
| label="Enter Bible Verse", | |
| placeholder="e.g., 'I am the vine, you are the branches' - John 15:5", | |
| lines=3 | |
| ), | |
| outputs=gr.Textbox(label="Figures of Speech Analysis (JSON)"), | |
| title="JR-Sacred Syntax: Bible Figures of Speech Detector", | |
| description="Enter a Bible verse to detect and analyze figures of speech with detailed biblical explanations." | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() | |