Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,16 @@
|
|
| 1 |
|
| 2 |
-
"""
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
import os
|
| 5 |
-
import spaces
|
| 6 |
import traceback
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
import gradio as gr
|
| 8 |
|
| 9 |
from rag_core import (
|
|
@@ -13,7 +20,6 @@ from rag_core import (
|
|
| 13 |
)
|
| 14 |
|
| 15 |
|
| 16 |
-
@spaces.GPU(duration=120)
|
| 17 |
def respond(question, retrieval_only):
|
| 18 |
"""Retrieve evidence and optionally generate an answer."""
|
| 19 |
|
|
@@ -31,30 +37,34 @@ def respond(question, retrieval_only):
|
|
| 31 |
)
|
| 32 |
|
| 33 |
return (
|
| 34 |
-
result
|
| 35 |
display_evidence(result),
|
| 36 |
-
result
|
| 37 |
)
|
| 38 |
|
| 39 |
except (ValueError, RuntimeError) as error:
|
| 40 |
traceback.print_exc()
|
|
|
|
| 41 |
return (
|
| 42 |
-
str(error),
|
| 43 |
"",
|
| 44 |
"Input or setup error"
|
| 45 |
)
|
| 46 |
|
| 47 |
-
except Exception:
|
| 48 |
traceback.print_exc()
|
|
|
|
| 49 |
return (
|
| 50 |
"Dataset/model access or generation failed. "
|
| 51 |
-
"
|
| 52 |
"",
|
| 53 |
"Model error"
|
| 54 |
)
|
| 55 |
|
| 56 |
|
| 57 |
def make_demo():
|
|
|
|
|
|
|
| 58 |
return gr.Interface(
|
| 59 |
fn=respond,
|
| 60 |
|
|
@@ -64,6 +74,7 @@ def make_demo():
|
|
| 64 |
placeholder="Which company produces the iPod?",
|
| 65 |
lines=2
|
| 66 |
),
|
|
|
|
| 67 |
gr.Checkbox(
|
| 68 |
label="Retrieval only (skip answer generation)",
|
| 69 |
value=False
|
|
@@ -72,15 +83,18 @@ def make_demo():
|
|
| 72 |
|
| 73 |
outputs=[
|
| 74 |
gr.Textbox(
|
| 75 |
-
label="Answer - verify
|
| 76 |
lines=5
|
| 77 |
),
|
|
|
|
| 78 |
gr.Textbox(
|
| 79 |
label="Source passages and similarity scores",
|
| 80 |
lines=15
|
| 81 |
),
|
|
|
|
| 82 |
gr.Textbox(
|
| 83 |
-
label="Run status"
|
|
|
|
| 84 |
)
|
| 85 |
],
|
| 86 |
|
|
@@ -98,23 +112,36 @@ def make_demo():
|
|
| 98 |
["Which company produces the iPod?", False],
|
| 99 |
["At what age did Frédéric move to Paris?", False],
|
| 100 |
["What is Beyoncé's private mobile phone number?", False]
|
| 101 |
-
]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
)
|
| 103 |
|
| 104 |
|
| 105 |
-
|
|
|
|
| 106 |
|
| 107 |
-
print("
|
|
|
|
|
|
|
|
|
|
| 108 |
|
| 109 |
try:
|
|
|
|
|
|
|
| 110 |
info = load_retriever()
|
| 111 |
-
|
|
|
|
| 112 |
|
| 113 |
except Exception:
|
| 114 |
print("Failed to initialize retriever.")
|
| 115 |
traceback.print_exc()
|
| 116 |
raise
|
| 117 |
|
|
|
|
|
|
|
| 118 |
demo = make_demo()
|
| 119 |
|
| 120 |
demo.queue(
|
|
@@ -122,6 +149,8 @@ if __name__ == "__main__":
|
|
| 122 |
max_size=20
|
| 123 |
)
|
| 124 |
|
|
|
|
|
|
|
| 125 |
demo.launch(
|
| 126 |
server_name="0.0.0.0",
|
| 127 |
server_port=int(os.environ.get("PORT", "7860")),
|
|
@@ -129,3 +158,7 @@ if __name__ == "__main__":
|
|
| 129 |
show_error=True,
|
| 130 |
ssr_mode=False
|
| 131 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
|
| 2 |
+
"""
|
| 3 |
+
Document Q&A with Manual RAG
|
| 4 |
+
Hugging Face Spaces - CPU Compatible Version
|
| 5 |
+
"""
|
| 6 |
|
| 7 |
import os
|
|
|
|
| 8 |
import traceback
|
| 9 |
+
|
| 10 |
+
# Force CPU execution before loading ML libraries
|
| 11 |
+
os.environ["CUDA_VISIBLE_DEVICES"] = ""
|
| 12 |
+
os.environ["TOKENIZERS_PARALLELISM"] = "false"
|
| 13 |
+
|
| 14 |
import gradio as gr
|
| 15 |
|
| 16 |
from rag_core import (
|
|
|
|
| 20 |
)
|
| 21 |
|
| 22 |
|
|
|
|
| 23 |
def respond(question, retrieval_only):
|
| 24 |
"""Retrieve evidence and optionally generate an answer."""
|
| 25 |
|
|
|
|
| 37 |
)
|
| 38 |
|
| 39 |
return (
|
| 40 |
+
result.get("answer", "No answer generated."),
|
| 41 |
display_evidence(result),
|
| 42 |
+
result.get("status", "Completed")
|
| 43 |
)
|
| 44 |
|
| 45 |
except (ValueError, RuntimeError) as error:
|
| 46 |
traceback.print_exc()
|
| 47 |
+
|
| 48 |
return (
|
| 49 |
+
f"Error: {str(error)}",
|
| 50 |
"",
|
| 51 |
"Input or setup error"
|
| 52 |
)
|
| 53 |
|
| 54 |
+
except Exception as error:
|
| 55 |
traceback.print_exc()
|
| 56 |
+
|
| 57 |
return (
|
| 58 |
"Dataset/model access or generation failed. "
|
| 59 |
+
f"Details: {str(error)}",
|
| 60 |
"",
|
| 61 |
"Model error"
|
| 62 |
)
|
| 63 |
|
| 64 |
|
| 65 |
def make_demo():
|
| 66 |
+
"""Create the Gradio interface."""
|
| 67 |
+
|
| 68 |
return gr.Interface(
|
| 69 |
fn=respond,
|
| 70 |
|
|
|
|
| 74 |
placeholder="Which company produces the iPod?",
|
| 75 |
lines=2
|
| 76 |
),
|
| 77 |
+
|
| 78 |
gr.Checkbox(
|
| 79 |
label="Retrieval only (skip answer generation)",
|
| 80 |
value=False
|
|
|
|
| 83 |
|
| 84 |
outputs=[
|
| 85 |
gr.Textbox(
|
| 86 |
+
label="Answer - verify against evidence",
|
| 87 |
lines=5
|
| 88 |
),
|
| 89 |
+
|
| 90 |
gr.Textbox(
|
| 91 |
label="Source passages and similarity scores",
|
| 92 |
lines=15
|
| 93 |
),
|
| 94 |
+
|
| 95 |
gr.Textbox(
|
| 96 |
+
label="Run status",
|
| 97 |
+
lines=2
|
| 98 |
)
|
| 99 |
],
|
| 100 |
|
|
|
|
| 112 |
["Which company produces the iPod?", False],
|
| 113 |
["At what age did Frédéric move to Paris?", False],
|
| 114 |
["What is Beyoncé's private mobile phone number?", False]
|
| 115 |
+
],
|
| 116 |
+
|
| 117 |
+
cache_examples=False,
|
| 118 |
+
|
| 119 |
+
flagging_mode="never"
|
| 120 |
)
|
| 121 |
|
| 122 |
|
| 123 |
+
def main():
|
| 124 |
+
"""Initialize retrieval system and launch the application."""
|
| 125 |
|
| 126 |
+
print("=" * 50)
|
| 127 |
+
print("Initializing Document Q&A with Manual RAG")
|
| 128 |
+
print("Execution mode: CPU")
|
| 129 |
+
print("=" * 50)
|
| 130 |
|
| 131 |
try:
|
| 132 |
+
print("Loading retriever...")
|
| 133 |
+
|
| 134 |
info = load_retriever()
|
| 135 |
+
|
| 136 |
+
print(f"Retriever initialized successfully: {info}")
|
| 137 |
|
| 138 |
except Exception:
|
| 139 |
print("Failed to initialize retriever.")
|
| 140 |
traceback.print_exc()
|
| 141 |
raise
|
| 142 |
|
| 143 |
+
print("Creating Gradio interface...")
|
| 144 |
+
|
| 145 |
demo = make_demo()
|
| 146 |
|
| 147 |
demo.queue(
|
|
|
|
| 149 |
max_size=20
|
| 150 |
)
|
| 151 |
|
| 152 |
+
print("Starting Gradio application...")
|
| 153 |
+
|
| 154 |
demo.launch(
|
| 155 |
server_name="0.0.0.0",
|
| 156 |
server_port=int(os.environ.get("PORT", "7860")),
|
|
|
|
| 158 |
show_error=True,
|
| 159 |
ssr_mode=False
|
| 160 |
)
|
| 161 |
+
|
| 162 |
+
|
| 163 |
+
if __name__ == "__main__":
|
| 164 |
+
main()
|