SatyamSinghal commited on
Commit
c7722c9
·
verified ·
1 Parent(s): 8a55271

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -28
app.py CHANGED
@@ -2,6 +2,7 @@ import os
2
  import gradio as gr
3
  import openai
4
  from langdetect import detect
 
5
 
6
  # Set up OpenAI API with your custom endpoint
7
  openai.api_key = os.getenv("API_KEY")
@@ -66,6 +67,34 @@ def get_groq_response(message, user_language):
66
  except Exception as e:
67
  return f"Oops, looks like something went wrong! Error: {str(e)}"
68
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
  # Function to handle the interaction and queries
70
  def market_analysis_agent(user_input, history=[]):
71
  try:
@@ -81,25 +110,28 @@ def market_analysis_agent(user_input, history=[]):
81
 
82
  # Handle private market queries with datasets
83
  if "company" in user_input.lower():
84
- response = format_response(company_profile)
85
  elif "financials" in user_input.lower():
86
- response = format_response(financials)
87
  elif "investors" in user_input.lower():
88
- response = format_response(investors)
89
  elif "products" in user_input.lower():
90
- response = format_response(products_services)
91
  elif "news" in user_input.lower() or "updates" in user_input.lower():
92
- response = format_response(news_updates)
93
  elif "legal" in user_input.lower() or "compliance" in user_input.lower():
94
- response = format_response(legal_compliance)
95
  elif "social media" in user_input.lower() or "instagram" in user_input.lower() or "linkedin" in user_input.lower() or "twitter" in user_input.lower():
96
- response = format_response(social_media)
97
  elif "workforce" in user_input.lower():
98
- response = format_response(workforce)
99
  else:
100
  # Get dynamic AI response if query doesn't match predefined terms
101
  response = get_groq_response(user_input, user_language)
102
 
 
 
 
103
  # Add some professional and engaging replies for the user
104
  cool_replies = [
105
  "That’s a great insight! Keep the questions coming. 🔍",
@@ -108,32 +140,15 @@ def market_analysis_agent(user_input, history=[]):
108
  "You've got the right focus. Let's sharpen those strategies. 🧠",
109
  "You're on the right track. Let’s optimize that idea! 🔧"
110
  ]
111
- response = f"{response} {cool_replies[hash(user_input) % len(cool_replies)]}"
112
-
113
  # Add to chat history
114
- history.append((user_input, response))
115
  return history, history
116
 
117
  except Exception as e:
118
  return [(user_input, f"Oops, something went wrong: {str(e)}")], history
119
 
120
- # Function to format dataset responses for readability and easy copying
121
- def format_response(data):
122
- # Check if the dataset is empty
123
- if not data:
124
- return "No data available for this query."
125
-
126
- formatted_response = "Here are the insights for your query:\n\n"
127
-
128
- # Loop through each dataset and format it in a readable manner
129
- for key, value in data.items():
130
- formatted_response += f"**{key.capitalize()}**:\n"
131
- for idx, item in enumerate(value, start=1):
132
- formatted_response += f"{idx}. {item}\n"
133
- formatted_response += "\n"
134
-
135
- return formatted_response.strip()
136
-
137
  # Gradio Interface setup
138
  chat_interface = gr.Interface(
139
  fn=market_analysis_agent, # Function for handling user interaction
 
2
  import gradio as gr
3
  import openai
4
  from langdetect import detect
5
+ import json
6
 
7
  # Set up OpenAI API with your custom endpoint
8
  openai.api_key = os.getenv("API_KEY")
 
67
  except Exception as e:
68
  return f"Oops, looks like something went wrong! Error: {str(e)}"
69
 
70
+ # Function to format the response data in a readable and copyable form
71
+ def format_response(data):
72
+ # Check if the dataset is empty or not in a format that can be iterated
73
+ if not data:
74
+ return "No data available for this query."
75
+
76
+ # Ensure that the data is iterable (list, dict, etc.)
77
+ if isinstance(data, dict):
78
+ formatted_response = "Here are the insights for your query:\n\n"
79
+
80
+ for key, value in data.items():
81
+ formatted_response += f"**{key.capitalize()}**:\n"
82
+ if isinstance(value, list): # Check if the value is a list
83
+ for idx, item in enumerate(value, start=1):
84
+ formatted_response += f"{idx}. {item}\n"
85
+ else:
86
+ formatted_response += f"{value}\n"
87
+ formatted_response += "\n"
88
+
89
+ elif isinstance(data, list): # If the data is a list
90
+ formatted_response = "Here are the insights for your query:\n\n"
91
+ for idx, item in enumerate(data, start=1):
92
+ formatted_response += f"{idx}. {item}\n"
93
+ else:
94
+ formatted_response = str(data)
95
+
96
+ return formatted_response.strip()
97
+
98
  # Function to handle the interaction and queries
99
  def market_analysis_agent(user_input, history=[]):
100
  try:
 
110
 
111
  # Handle private market queries with datasets
112
  if "company" in user_input.lower():
113
+ response = company_profile
114
  elif "financials" in user_input.lower():
115
+ response = financials
116
  elif "investors" in user_input.lower():
117
+ response = investors
118
  elif "products" in user_input.lower():
119
+ response = products_services
120
  elif "news" in user_input.lower() or "updates" in user_input.lower():
121
+ response = news_updates
122
  elif "legal" in user_input.lower() or "compliance" in user_input.lower():
123
+ response = legal_compliance
124
  elif "social media" in user_input.lower() or "instagram" in user_input.lower() or "linkedin" in user_input.lower() or "twitter" in user_input.lower():
125
+ response = social_media
126
  elif "workforce" in user_input.lower():
127
+ response = workforce
128
  else:
129
  # Get dynamic AI response if query doesn't match predefined terms
130
  response = get_groq_response(user_input, user_language)
131
 
132
+ # Format the response for easy readability and copy-pasting
133
+ formatted_response = format_response(response)
134
+
135
  # Add some professional and engaging replies for the user
136
  cool_replies = [
137
  "That’s a great insight! Keep the questions coming. 🔍",
 
140
  "You've got the right focus. Let's sharpen those strategies. 🧠",
141
  "You're on the right track. Let’s optimize that idea! 🔧"
142
  ]
143
+ formatted_response += f"\n{cool_replies[hash(user_input) % len(cool_replies)]}"
144
+
145
  # Add to chat history
146
+ history.append((user_input, formatted_response))
147
  return history, history
148
 
149
  except Exception as e:
150
  return [(user_input, f"Oops, something went wrong: {str(e)}")], history
151
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
152
  # Gradio Interface setup
153
  chat_interface = gr.Interface(
154
  fn=market_analysis_agent, # Function for handling user interaction