Spaces:
Sleeping
Sleeping
techy-ai commited on
Commit ·
b3a3adf
1
Parent(s): 500e8e6
ui added
Browse files- Gradio_UI.py +79 -0
- agent.py +7 -7
Gradio_UI.py
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import json
|
| 3 |
+
from agent import build_graph
|
| 4 |
+
from langchain_core.messages import HumanMessage
|
| 5 |
+
|
| 6 |
+
# Load questions from metadata.jsonl
|
| 7 |
+
def load_questions(jsonl_path):
|
| 8 |
+
questions = []
|
| 9 |
+
with open(jsonl_path, 'r', encoding='utf-8') as f:
|
| 10 |
+
for line in f:
|
| 11 |
+
try:
|
| 12 |
+
obj = json.loads(line)
|
| 13 |
+
if 'Question' in obj:
|
| 14 |
+
questions.append(obj['Question'])
|
| 15 |
+
except Exception:
|
| 16 |
+
continue
|
| 17 |
+
return questions
|
| 18 |
+
|
| 19 |
+
questions_list = load_questions('metadata.jsonl')
|
| 20 |
+
# Basic filter for violence/intimate (simple keyword check)
|
| 21 |
+
def is_safe_question(q):
|
| 22 |
+
unsafe_keywords = ['kill', 'murder', 'sex', 'intimate', 'violence', 'abuse']
|
| 23 |
+
q_lower = q.lower()
|
| 24 |
+
return not any(word in q_lower for word in unsafe_keywords)
|
| 25 |
+
|
| 26 |
+
# Backend logic: send question to agent
|
| 27 |
+
def get_answer(question, history):
|
| 28 |
+
if not is_safe_question(question):
|
| 29 |
+
return "Sorry, this question is not allowed.", history
|
| 30 |
+
try:
|
| 31 |
+
graph = build_graph(provider="groq")
|
| 32 |
+
messages = [HumanMessage(content=question)]
|
| 33 |
+
result = graph.invoke({"messages": messages})
|
| 34 |
+
# Get last message as answer
|
| 35 |
+
answer = result["messages"][-1].content if result["messages"] else "No answer."
|
| 36 |
+
history = history + [(question, answer)]
|
| 37 |
+
return answer, history
|
| 38 |
+
except Exception as e:
|
| 39 |
+
return f"Error: {str(e)}", history
|
| 40 |
+
|
| 41 |
+
# Gradio UI
|
| 42 |
+
with gr.Blocks(theme=gr.themes.Soft(primary_hue="purple", secondary_hue="purple")) as demo:
|
| 43 |
+
gr.Markdown("# AI Agent Q&A", elem_id="title")
|
| 44 |
+
gr.Markdown("Ask your own question or select one from the list below.")
|
| 45 |
+
with gr.Row():
|
| 46 |
+
question_box = gr.Textbox(label="Type your question", lines=3)
|
| 47 |
+
question_list = gr.Dropdown(choices=questions_list, label="Or choose a question", interactive=True)
|
| 48 |
+
submit_btn = gr.Button("Submit", elem_id="submit-btn")
|
| 49 |
+
reset_btn = gr.Button("Reset", elem_id="reset-btn")
|
| 50 |
+
answer_box = gr.Textbox(label="Answer", interactive=False)
|
| 51 |
+
with gr.Accordion("Show previous Q&A", open=False):
|
| 52 |
+
history_box = gr.Dataframe(headers=["Question", "Answer"], datatype=["str", "str"], interactive=False)
|
| 53 |
+
|
| 54 |
+
state = gr.State([])
|
| 55 |
+
def submit_fn(q_text, q_list, history):
|
| 56 |
+
question = q_text if q_text else q_list
|
| 57 |
+
if not question:
|
| 58 |
+
return "Please enter or select a question.", history
|
| 59 |
+
return get_answer(question, history)
|
| 60 |
+
|
| 61 |
+
def reset_fn():
|
| 62 |
+
return "", []
|
| 63 |
+
|
| 64 |
+
submit_btn.click(
|
| 65 |
+
submit_fn,
|
| 66 |
+
inputs=[question_box, question_list, state],
|
| 67 |
+
outputs=[answer_box, state],
|
| 68 |
+
api_name="submit",
|
| 69 |
+
)
|
| 70 |
+
reset_btn.click(
|
| 71 |
+
reset_fn,
|
| 72 |
+
inputs=[],
|
| 73 |
+
outputs=[answer_box, state],
|
| 74 |
+
api_name="reset",
|
| 75 |
+
)
|
| 76 |
+
state.change(lambda h: h, inputs=state, outputs=history_box)
|
| 77 |
+
|
| 78 |
+
if __name__ == "__main__":
|
| 79 |
+
demo.launch()
|
agent.py
CHANGED
|
@@ -864,11 +864,11 @@ def build_graph(provider: str = "groq"):
|
|
| 864 |
|
| 865 |
|
| 866 |
# test
|
| 867 |
-
if __name__ == "__main__":
|
| 868 |
-
|
| 869 |
-
|
| 870 |
-
|
| 871 |
-
|
| 872 |
-
|
| 873 |
-
|
| 874 |
|
|
|
|
| 864 |
|
| 865 |
|
| 866 |
# test
|
| 867 |
+
# if __name__ == "__main__":
|
| 868 |
+
# question = "When was a picture of St. Thomas Aquinas first added to the Wikipedia page on the Principle of double effect?"
|
| 869 |
+
# graph = build_graph(provider="groq")
|
| 870 |
+
# messages = [HumanMessage(content=question)]
|
| 871 |
+
# messages = graph.invoke({"messages": messages})
|
| 872 |
+
# for m in messages["messages"]:
|
| 873 |
+
# m.pretty_print()
|
| 874 |
|