Spaces:
Build error
Build error
File size: 2,658 Bytes
823834e aa94de3 823834e a576072 aa94de3 5b14ccc 25d3696 5b14ccc 25d3696 5b14ccc 25d3696 823834e fbc8dab 39d5100 5b14ccc eebcadd 5b14ccc 25d3696 fbc8dab 1488a70 823834e fbc8dab 5b14ccc 1488a70 5b14ccc 1488a70 5b14ccc 1488a70 5b14ccc a576072 39d5100 7d42faa a576072 5b14ccc 1488a70 5b14ccc aa94de3 5b14ccc |
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 |
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"""<s>[INST] You are JR-Sacred Syntax, an expert biblical scholar specializing in Bible analysis.
Analyze the following Bible verse to identify any figures of speech (e.g., Metaphor, Synecdoche, Hyperbole, Simile, Paradox).
Return your answer as a JSON array where each element includes:
- "verses": the Bible verse reference,
- "figure": the type of figure of speech,
- "phrase": the exact phrase from the verse,
- "explanation": a brief explanation in biblical context.
Bible Verse: "{verse}"
[/INST] Figures:</s>"""
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()
|