rknl commited on
Commit
dd91dc5
·
verified ·
1 Parent(s): 95325f5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -66
app.py CHANGED
@@ -51,81 +51,61 @@ def query_tqa(query, search_level):
51
  # )
52
  print(str(grag_response.response))
53
  return (
54
- str(grag_response.response),
55
- # grag_reference,
56
- # grag_reference_text,
57
- # rag_response,
58
- # rag_reference,
59
- # rag_reference_text,
60
  )
61
 
62
 
63
- def show_graph():
64
- """
65
- Show the latest graph visualization in an iframe.
66
-
67
- Returns:
68
- str: The HTML content to display the graph visualization in an iframe.
69
- """
70
-
71
- graph_vis_dir = os.getenv("GRAPH_VIS", "graph_vis")
72
- try:
73
- latest_graph = get_latest_html_file(graph_vis_dir)
74
- if latest_graph:
75
- with open(latest_graph, "r", encoding="utf-8") as f:
76
- html_content = f.read()
77
 
78
- encoded_html = base64.b64encode(html_content.encode()).decode()
79
- iframe_html = f'<iframe src="data:text/html;base64,{encoded_html}" width="100%" height="1000px" frameborder="0"></iframe>'
80
- return iframe_html
81
- else:
82
- return "No graph visualization found."
83
- except Exception as e:
84
- return f"Error: {str(e)}"
85
-
86
-
87
- def reveal_coupon(query, grag_response):
88
- """
89
- Get the coupon from the query and response.
 
 
 
 
 
 
 
 
 
 
 
 
 
90
 
91
- Args:
92
- query (str): Query asked to Graph-RAG.
93
- grag_response (str): Response from the Graph-RAG model.
 
 
94
 
95
- Returns:
96
- str: Coupon with reasoning.
97
- """
 
 
98
 
99
- if not query.strip() or not grag_response.strip():
100
- raise gr.Error("Please ask a query and get a response before revealing the coupon.")
101
 
102
- coupon = get_coupon(query, grag_response)
103
- return coupon
 
 
104
 
105
-
106
- with gr.Blocks() as demo:
107
- gr.Markdown("# Comfy Virtual Assistant")
108
- chatbot = gr.Chatbot(
109
- label="Comfy Virtual Assistant",
110
- type="messages",
111
- scale=1,
112
- # suggestions = [
113
- # {"text": "How much iphone cost?"},
114
- # {"text": "What phone options do i have ?"}
115
- # ],
116
-
117
  )
118
- msg = gr.Textbox(label="Input Your Query")
119
- clear = gr.ClearButton([msg, chatbot])
120
-
121
- def respond(message, chat_history):
122
- bot_message = query_tqa(message, 2)
123
- # chat_history.append((message, bot_message))
124
- chat_history.append(ChatMessage(role="user", content=message))
125
- chat_history.append(ChatMessage(role="assistant", content=bot_message))
126
- time.sleep(1)
127
- return "", chat_history
128
 
129
- msg.submit(respond, [msg, chatbot], [msg, chatbot])
130
-
131
  demo.launch(auth=(os.getenv("ID"), os.getenv("PASS")), share=False)
 
51
  # )
52
  print(str(grag_response.response))
53
  return (
54
+ str(grag_response.response)
 
 
 
 
 
55
  )
56
 
57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
 
59
+
60
+ # with gr.Blocks() as demo:
61
+ # gr.Markdown("# Comfy Virtual Assistant")
62
+ # chatbot = gr.Chatbot(
63
+ # label="Comfy Virtual Assistant",
64
+ # type="messages",
65
+ # scale=1,
66
+ # # suggestions = [
67
+ # # {"text": "How much iphone cost?"},
68
+ # # {"text": "What phone options do i have ?"}
69
+ # # ],
70
+
71
+ # )
72
+ # msg = gr.Textbox(label="Input Your Query")
73
+ # clear = gr.ClearButton([msg, chatbot])
74
+
75
+ # def respond(message, chat_history):
76
+ # bot_message = query_tqa(message, 2)
77
+ # # chat_history.append((message, bot_message))
78
+ # chat_history.append(ChatMessage(role="user", content=message))
79
+ # chat_history.append(ChatMessage(role="assistant", content=bot_message))
80
+ # time.sleep(1)
81
+ # return "", chat_history
82
+
83
+ # msg.submit(respond, [msg, chatbot], [msg, chatbot])
84
 
85
+ def chatbot_response(message: str, history: List[Tuple[str, str]]) -> str:
86
+ # Use the query_tqa function to get the response
87
+ search_level = 2 # You can adjust this or make it configurable
88
+ response = query_tqa(message, search_level)
89
+ return response
90
 
91
+ # Create the Gradio interface
92
+ with gr.Blocks() as demo:
93
+ chatbot = gr.Chatbot()
94
+ msg = gr.Textbox()
95
+ clear = gr.Button("Clear")
96
 
97
+ def user(user_message, history):
98
+ return "", history + [[user_message, None]]
99
 
100
+ def bot(history):
101
+ bot_message = chatbot_response(history[-1][0], history)
102
+ history[-1][1] = bot_message
103
+ return history
104
 
105
+ msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
106
+ bot, chatbot, chatbot
 
 
 
 
 
 
 
 
 
 
107
  )
108
+ clear.click(lambda: None, None, chatbot, queue=False)
109
+
 
 
 
 
 
 
 
 
110
 
 
 
111
  demo.launch(auth=(os.getenv("ID"), os.getenv("PASS")), share=False)