Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,8 +2,8 @@ import os
|
|
2 |
import gradio as gr
|
3 |
import requests
|
4 |
import json
|
5 |
-
# import logging
|
6 |
import logging
|
|
|
7 |
# Retrieve the API token from secrets
|
8 |
api_token = os.getenv("API_TOKEN")
|
9 |
if not api_token:
|
@@ -13,16 +13,10 @@ if not api_token:
|
|
13 |
logging.basicConfig(level=logging.INFO)
|
14 |
logger = logging.getLogger(__name__)
|
15 |
|
16 |
-
# Retrieve the API token from secrets
|
17 |
-
api_token = os.getenv("API_TOKEN")
|
18 |
-
if not api_token:
|
19 |
-
raise ValueError("API token not found. Make sure 'API_TOKEN' is set in the Secrets.")
|
20 |
-
|
21 |
# Use the token in your request headers
|
22 |
-
API_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.3"
|
23 |
HEADERS = {"Authorization": f"Bearer {api_token}"}
|
24 |
|
25 |
-
|
26 |
def generate_exegesis(passage):
|
27 |
if not passage.strip():
|
28 |
return "Please enter a Bible passage."
|
@@ -38,13 +32,12 @@ def generate_exegesis(passage):
|
|
38 |
response = requests.post(API_URL, headers=HEADERS, json=payload)
|
39 |
response.raise_for_status()
|
40 |
result = response.json()
|
41 |
-
logger.debug("Full API Response: %s", json.dumps(result, indent=4))
|
42 |
|
43 |
if isinstance(result, list) and len(result) > 0:
|
44 |
generated_text = result[0].get("generated_text", "")
|
45 |
-
marker = "Exegesis:"
|
46 |
if marker in generated_text:
|
47 |
-
# Return only the text after the marker
|
48 |
generated_text = generated_text.split(marker, 1)[1].strip()
|
49 |
return generated_text or "Error: No response from model."
|
50 |
else:
|
@@ -65,13 +58,12 @@ def ask_any_questions(question):
|
|
65 |
response = requests.post(API_URL, headers=HEADERS, json=payload)
|
66 |
response.raise_for_status()
|
67 |
result = response.json()
|
68 |
-
logger.debug("Full API Response: %s", json.dumps(result, indent=4))
|
69 |
|
70 |
if isinstance(result, list) and len(result) > 0:
|
71 |
answer_text = result[0].get("generated_text", "")
|
72 |
-
marker = "Answer:"
|
73 |
if marker in answer_text:
|
74 |
-
# Return only the text after the marker
|
75 |
answer_text = answer_text.split(marker, 1)[1].strip()
|
76 |
return answer_text or "Error: No response from model."
|
77 |
else:
|
@@ -92,13 +84,12 @@ def generate_sermon(topic):
|
|
92 |
response = requests.post(API_URL, headers=HEADERS, json=payload)
|
93 |
response.raise_for_status()
|
94 |
result = response.json()
|
95 |
-
|
96 |
|
97 |
if isinstance(result, list) and len(result) > 0:
|
98 |
sermon_text = result[0].get("generated_text", "")
|
99 |
-
marker = "Sermon:"
|
100 |
if marker in sermon_text:
|
101 |
-
# Return only the text after the marker
|
102 |
sermon_text = sermon_text.split(marker, 1)[1].strip()
|
103 |
return sermon_text or "Error: No response from model."
|
104 |
else:
|
@@ -119,13 +110,12 @@ def keyword_search(keyword):
|
|
119 |
response = requests.post(API_URL, headers=HEADERS, json=payload)
|
120 |
response.raise_for_status()
|
121 |
result = response.json()
|
122 |
-
|
123 |
|
124 |
if isinstance(result, list) and len(result) > 0:
|
125 |
search_results = result[0].get("generated_text", "")
|
126 |
-
marker = "Search Results:"
|
127 |
if marker in search_results:
|
128 |
-
# Return only the text after the marker
|
129 |
search_results = search_results.split(marker, 1)[1].strip()
|
130 |
return search_results or "Error: No response from model."
|
131 |
else:
|
@@ -173,4 +163,4 @@ bible_app = gr.TabbedInterface(
|
|
173 |
)
|
174 |
|
175 |
if __name__ == "__main__":
|
176 |
-
bible_app.launch()
|
|
|
2 |
import gradio as gr
|
3 |
import requests
|
4 |
import json
|
|
|
5 |
import logging
|
6 |
+
|
7 |
# Retrieve the API token from secrets
|
8 |
api_token = os.getenv("API_TOKEN")
|
9 |
if not api_token:
|
|
|
13 |
logging.basicConfig(level=logging.INFO)
|
14 |
logger = logging.getLogger(__name__)
|
15 |
|
|
|
|
|
|
|
|
|
|
|
16 |
# Use the token in your request headers
|
17 |
+
API_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.3"
|
18 |
HEADERS = {"Authorization": f"Bearer {api_token}"}
|
19 |
|
|
|
20 |
def generate_exegesis(passage):
|
21 |
if not passage.strip():
|
22 |
return "Please enter a Bible passage."
|
|
|
32 |
response = requests.post(API_URL, headers=HEADERS, json=payload)
|
33 |
response.raise_for_status()
|
34 |
result = response.json()
|
35 |
+
logger.debug("Full API Response: %s", json.dumps(result, indent=4))
|
36 |
|
37 |
if isinstance(result, list) and len(result) > 0:
|
38 |
generated_text = result[0].get("generated_text", "")
|
39 |
+
marker = "Exegesis:"
|
40 |
if marker in generated_text:
|
|
|
41 |
generated_text = generated_text.split(marker, 1)[1].strip()
|
42 |
return generated_text or "Error: No response from model."
|
43 |
else:
|
|
|
58 |
response = requests.post(API_URL, headers=HEADERS, json=payload)
|
59 |
response.raise_for_status()
|
60 |
result = response.json()
|
61 |
+
logger.debug("Full API Response: %s", json.dumps(result, indent=4))
|
62 |
|
63 |
if isinstance(result, list) and len(result) > 0:
|
64 |
answer_text = result[0].get("generated_text", "")
|
65 |
+
marker = "Answer:"
|
66 |
if marker in answer_text:
|
|
|
67 |
answer_text = answer_text.split(marker, 1)[1].strip()
|
68 |
return answer_text or "Error: No response from model."
|
69 |
else:
|
|
|
84 |
response = requests.post(API_URL, headers=HEADERS, json=payload)
|
85 |
response.raise_for_status()
|
86 |
result = response.json()
|
87 |
+
logger.debug("Full API Response: %s", json.dumps(result, indent=4))
|
88 |
|
89 |
if isinstance(result, list) and len(result) > 0:
|
90 |
sermon_text = result[0].get("generated_text", "")
|
91 |
+
marker = "Sermon:"
|
92 |
if marker in sermon_text:
|
|
|
93 |
sermon_text = sermon_text.split(marker, 1)[1].strip()
|
94 |
return sermon_text or "Error: No response from model."
|
95 |
else:
|
|
|
110 |
response = requests.post(API_URL, headers=HEADERS, json=payload)
|
111 |
response.raise_for_status()
|
112 |
result = response.json()
|
113 |
+
logger.debug("Full API Response: %s", json.dumps(result, indent=4))
|
114 |
|
115 |
if isinstance(result, list) and len(result) > 0:
|
116 |
search_results = result[0].get("generated_text", "")
|
117 |
+
marker = "Search Results:"
|
118 |
if marker in search_results:
|
|
|
119 |
search_results = search_results.split(marker, 1)[1].strip()
|
120 |
return search_results or "Error: No response from model."
|
121 |
else:
|
|
|
163 |
)
|
164 |
|
165 |
if __name__ == "__main__":
|
166 |
+
bible_app.launch()
|