mou3az commited on
Commit
0cf95f2
1 Parent(s): ee472e2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -12
app.py CHANGED
@@ -29,31 +29,31 @@ messages = [
29
  AIMessage(content="I'm great thank you. How can I help you?")
30
  ]
31
 
32
- def handle_query(query: str, mode: str):
33
 
34
  # Check if query is empty
35
- if not query.strip():
36
  return "Enter a valid message."
37
  if mode == "Chat":
38
- return chat_mode(query)
39
  elif mode == "Web-Search":
40
- return web_search(query)
41
  else:
42
  return "Select a valid mode."
43
 
44
- def chat_mode(query: str):
45
  global messages
46
- prompt = HumanMessage(content=query)
47
  messages.append(prompt)
48
  response = chat_model.invoke(messages)
49
  messages.append(response.content)
50
  if len(messages) >= 6:
51
  messages = messages[-6:]
52
- return f"You: {query}\n\nIT-Assistant: {response.content}"
53
 
54
- def web_search(query: str):
55
  global messages
56
- similar_docs = db.similarity_search(query, k=3)
57
  if similar_docs:
58
  source_knowledge = "\n".join([x.page_content for x in similar_docs])
59
  else:
@@ -61,7 +61,7 @@ def web_search(query: str):
61
  augmented_prompt = f"""
62
  If the answer to the next query is not contained in the Web Search say 'No Answer Available' and then give advice.
63
 
64
- Query: {query}
65
 
66
  Web Search:
67
  {source_knowledge}
@@ -72,10 +72,10 @@ def web_search(query: str):
72
  messages.append(response.content)
73
  if len(messages) >= 6:
74
  messages = messages[-6:]
75
- return f"You: {query}\n\nIT-Assistant: {response.content}"
76
 
77
  demo = gr.Interface(
78
- fn=handle_query,
79
  inputs=["text", gr.Radio(["Chat", "Web-Search"], label="Mode", info="Choose a mode and enter your message, then click submit to interact.")],
80
  outputs="text",
81
  title="IT Assistant")
 
29
  AIMessage(content="I'm great thank you. How can I help you?")
30
  ]
31
 
32
+ def handle_message(message: str, mode: str):
33
 
34
  # Check if query is empty
35
+ if not message.strip():
36
  return "Enter a valid message."
37
  if mode == "Chat":
38
+ return chat_mode(message)
39
  elif mode == "Web-Search":
40
+ return web_search(message)
41
  else:
42
  return "Select a valid mode."
43
 
44
+ def chat_mode(message: str):
45
  global messages
46
+ prompt = HumanMessage(content=message)
47
  messages.append(prompt)
48
  response = chat_model.invoke(messages)
49
  messages.append(response.content)
50
  if len(messages) >= 6:
51
  messages = messages[-6:]
52
+ return f"You: {message}\n\nIT-Assistant: {response.content}"
53
 
54
+ def web_search(message: str):
55
  global messages
56
+ similar_docs = db.similarity_search(message, k=3)
57
  if similar_docs:
58
  source_knowledge = "\n".join([x.page_content for x in similar_docs])
59
  else:
 
61
  augmented_prompt = f"""
62
  If the answer to the next query is not contained in the Web Search say 'No Answer Available' and then give advice.
63
 
64
+ Query: {message}
65
 
66
  Web Search:
67
  {source_knowledge}
 
72
  messages.append(response.content)
73
  if len(messages) >= 6:
74
  messages = messages[-6:]
75
+ return f"You: {message}\n\nIT-Assistant: {response.content}"
76
 
77
  demo = gr.Interface(
78
+ fn=handle_message,
79
  inputs=["text", gr.Radio(["Chat", "Web-Search"], label="Mode", info="Choose a mode and enter your message, then click submit to interact.")],
80
  outputs="text",
81
  title="IT Assistant")