mou3az commited on
Commit
f9d1bd8
1 Parent(s): 7207714

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -6
app.py CHANGED
@@ -1,4 +1,6 @@
1
  import gradio as gr
 
 
2
  from langchain.embeddings import SentenceTransformerEmbeddings
3
  from langchain.vectorstores import FAISS
4
  from langchain_community.chat_models.huggingface import ChatHuggingFace
@@ -34,14 +36,16 @@ def handle_message(message: str, mode: str):
34
  # Check if message 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)
@@ -49,7 +53,7 @@ def chat_mode(message: str):
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
@@ -72,11 +76,41 @@ def web_search(message: str):
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")
82
  demo.launch()
 
1
  import gradio as gr
2
+ import requests
3
+ from IPython.display import Image, display
4
  from langchain.embeddings import SentenceTransformerEmbeddings
5
  from langchain.vectorstores import FAISS
6
  from langchain_community.chat_models.huggingface import ChatHuggingFace
 
36
  # Check if message is empty
37
  if not message.strip():
38
  return "Enter a valid message."
39
+ if mode == "Chat-Message":
40
+ return chat_message(message)
41
  elif mode == "Web-Search":
42
  return web_search(message)
43
+ elif mode == "Chart-Generator":
44
+ return chart_generator(message)
45
  else:
46
  return "Select a valid mode."
47
 
48
+ def chat_message(message: str):
49
  global messages
50
  prompt = HumanMessage(content=message)
51
  messages.append(prompt)
 
53
  messages.append(response.content)
54
  if len(messages) >= 6:
55
  messages = messages[-6:]
56
+ return f"IT-Assistant: {response.content}"
57
 
58
  def web_search(message: str):
59
  global messages
 
76
  messages.append(response.content)
77
  if len(messages) >= 6:
78
  messages = messages[-6:]
79
+ return f"IT-Assistant: {response.content}"
80
 
81
+ def chart_generator(message: str):
82
+ # Construct the full chart URL
83
+ chart_url = f"https://quickchart.io/natural/{message}"
84
+
85
+ # Send a GET request to the chart URL
86
+ response = requests.get(chart_url)
87
+
88
+ # Check if the request was successful (status code 200)
89
+ if response.status_code == 200:
90
+ # Add a description to the message
91
+ message_with_description = f"Describe and analyse the content of this chart: {message}"
92
+
93
+ # Create a HumanMessage object with the message including the description
94
+ prompt = HumanMessage(content=message_with_description)
95
+ messages.append(prompt)
96
+
97
+ # Invoke the chat model with the updated messages
98
+ response = chat_model.invoke(messages)
99
+ messages.append(response.content)
100
+ if len(messages) >= 6:
101
+ messages = messages[-6:]
102
+
103
+ # Display the chart image with a smaller size
104
+ display(Image(url=chart_url, width=500, height=300))
105
+
106
+ # Return the message and response
107
+ return f"IT-Assistant: {response.content}"
108
+ else:
109
+ return f"Can't generate this image. Please provide valid chart details."
110
+
111
  demo = gr.Interface(
112
  fn=handle_message,
113
+ inputs=["text", gr.Radio(["Chat", "Web-Search", "Chart-Generator"], label="mode", info="Choose a mode and enter your message, then click submit to interact.")],
114
  outputs="text",
115
  title="IT Assistant")
116
  demo.launch()