Josebert commited on
Commit
aa94de3
·
verified ·
1 Parent(s): cef95a0

Create App.py

Browse files
Files changed (1) hide show
  1. App.py +72 -0
App.py ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import gradio as gr
3
+ from huggingface_hub import InferenceClient
4
+
5
+ # Initialize the client using the specified model.
6
+ client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
7
+
8
+ def respond(message, history: list[tuple[str, str]], system_message, max_tokens, temperature, top_p):
9
+ """
10
+ Builds a conversation history that includes a system instruction for Bible analysis.
11
+
12
+ The system message instructs the model to act as JR‑Sacred Syntax, an expert biblical scholar
13
+ specializing in analyzing Bible verses to detect figures of speech (e.g., Metaphor, Synecdoche, Hyperbole, etc.)
14
+ and output a JSON array with each entry containing:
15
+ - "figure": the type of figure of speech,
16
+ - "phrase": the phrase in the verse,
17
+ - "explanation": explanation in biblical context.
18
+ """
19
+ # Build the message list starting with the system message.
20
+ messages = [{"role": "system", "content": system_message}]
21
+
22
+ # Append previous conversation history.
23
+ for user_msg, assistant_msg in history:
24
+ if user_msg:
25
+ messages.append({"role": "user", "content": user_msg})
26
+ if assistant_msg:
27
+ messages.append({"role": "assistant", "content": assistant_msg})
28
+
29
+ # Append the new user message.
30
+ messages.append({"role": "user", "content": message})
31
+
32
+ response = ""
33
+ # Stream the chat completion response.
34
+ for chat_message in client.chat_completion(
35
+ messages,
36
+ max_tokens=max_tokens,
37
+ stream=True,
38
+ temperature=temperature,
39
+ top_p=top_p,
40
+ ):
41
+ token = chat_message.choices[0].delta.content
42
+ response += token
43
+ yield response
44
+
45
+ # Define a default system message that guides the model for Bible figure-of-speech analysis.
46
+ default_system_message = (
47
+ "You are JR-Sacred Syntax, an expert biblical scholar. "
48
+ "When given a Bible verse, analyze it to detect any figures of speech (such as Metaphor, "
49
+ "Synecdoche, Hyperbole, Simile, or Paradox) and return a JSON array where each entry includes "
50
+ "'figure' (the type), 'phrase' (the exact words from the verse), and 'explanation' (why it qualifies as such) "
51
+ "in a biblical context. Respond only in JSON format."
52
+ )
53
+
54
+ # Create the Gradio Chat Interface.
55
+ demo = gr.ChatInterface(
56
+ respond,
57
+ additional_inputs=[
58
+ gr.Textbox(value=default_system_message, label="System Message (Instruction)"),
59
+ gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max New Tokens"),
60
+ gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
61
+ gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (Nucleus Sampling)"),
62
+ ],
63
+ title="JR‑Sacred Syntax: Bible Figures of Speech Detector",
64
+ description=(
65
+ "Enter a Bible verse to have it analyzed for figures of speech. "
66
+ "JR‑Sacred Syntax will detect and explain literary devices in the verse, "
67
+ "returning the results in a structured JSON format."
68
+ )
69
+ )
70
+
71
+ if __name__ == "__main__":
72
+ demo.launch()