Josebert's picture
Create App.py
aa94de3 verified
raw
history blame
3.05 kB
import json
import gradio as gr
from huggingface_hub import InferenceClient
# Initialize the client using the specified model.
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
def respond(message, history: list[tuple[str, str]], system_message, max_tokens, temperature, top_p):
"""
Builds a conversation history that includes a system instruction for Bible analysis.
The system message instructs the model to act as JR‑Sacred Syntax, an expert biblical scholar
specializing in analyzing Bible verses to detect figures of speech (e.g., Metaphor, Synecdoche, Hyperbole, etc.)
and output a JSON array with each entry containing:
- "figure": the type of figure of speech,
- "phrase": the phrase in the verse,
- "explanation": explanation in biblical context.
"""
# Build the message list starting with the system message.
messages = [{"role": "system", "content": system_message}]
# Append previous conversation history.
for user_msg, assistant_msg in history:
if user_msg:
messages.append({"role": "user", "content": user_msg})
if assistant_msg:
messages.append({"role": "assistant", "content": assistant_msg})
# Append the new user message.
messages.append({"role": "user", "content": message})
response = ""
# Stream the chat completion response.
for chat_message in client.chat_completion(
messages,
max_tokens=max_tokens,
stream=True,
temperature=temperature,
top_p=top_p,
):
token = chat_message.choices[0].delta.content
response += token
yield response
# Define a default system message that guides the model for Bible figure-of-speech analysis.
default_system_message = (
"You are JR-Sacred Syntax, an expert biblical scholar. "
"When given a Bible verse, analyze it to detect any figures of speech (such as Metaphor, "
"Synecdoche, Hyperbole, Simile, or Paradox) and return a JSON array where each entry includes "
"'figure' (the type), 'phrase' (the exact words from the verse), and 'explanation' (why it qualifies as such) "
"in a biblical context. Respond only in JSON format."
)
# Create the Gradio Chat Interface.
demo = gr.ChatInterface(
respond,
additional_inputs=[
gr.Textbox(value=default_system_message, label="System Message (Instruction)"),
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max New Tokens"),
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (Nucleus Sampling)"),
],
title="JR‑Sacred Syntax: Bible Figures of Speech Detector",
description=(
"Enter a Bible verse to have it analyzed for figures of speech. "
"JR‑Sacred Syntax will detect and explain literary devices in the verse, "
"returning the results in a structured JSON format."
)
)
if __name__ == "__main__":
demo.launch()