Manavraj commited on
Commit
e0823ce
·
verified ·
1 Parent(s): c0f3971

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -27
app.py CHANGED
@@ -4,6 +4,11 @@ from bs4 import BeautifulSoup
4
  import json
5
  from urllib.parse import quote_plus
6
  import time
 
 
 
 
 
7
 
8
  def search_knowledge_base(issue):
9
  """
@@ -13,12 +18,18 @@ def search_knowledge_base(issue):
13
  Returns:
14
  str: Solution steps or message if no solution found
15
  """
16
- issue = issue.lower()
17
- if "wifi" in issue:
18
- return "Check if your router is powered on. Restart your router. Try connecting again."
19
- elif "screen" in issue:
20
- return "Adjust display cable. Update graphics drivers. Restart the system."
21
- return "No predefined solution found in the knowledge base."
 
 
 
 
 
 
22
 
23
  def search_web_duckduckgo(query):
24
  """
@@ -60,8 +71,10 @@ def search_web_duckduckgo(query):
60
  return f"Search completed for '{query}' but no detailed results available. Try a more specific query."
61
 
62
  except requests.RequestException as e:
 
63
  return f"Error performing web search: {str(e)}"
64
  except Exception as e:
 
65
  return f"Unexpected error: {str(e)}"
66
 
67
  def search_web_scraper(query):
@@ -100,6 +113,7 @@ def search_web_scraper(query):
100
  return f"No results found for '{query}'. Try different keywords."
101
 
102
  except Exception as e:
 
103
  return f"Error in backup search: {str(e)}"
104
 
105
  def search_web(query):
@@ -110,18 +124,22 @@ def search_web(query):
110
  Returns:
111
  str: Formatted search results
112
  """
113
- if not query.strip():
114
- return "Please enter a search query."
115
-
116
- # Try DuckDuckGo API first
117
- result = search_web_duckduckgo(query)
118
-
119
- # If API fails, try scraping method
120
- if "Error" in result:
121
- print("API method failed, trying scraping method...")
122
- result = search_web_scraper(query)
123
-
124
- return result
 
 
 
 
125
 
126
  def format_response(raw_steps):
127
  """
@@ -131,12 +149,18 @@ def format_response(raw_steps):
131
  Returns:
132
  str: Formatted numbered list of steps
133
  """
134
- if not raw_steps.strip():
135
- return "Please enter some text to format."
136
-
137
- steps = raw_steps.split(". ")
138
- steps = [f"{i+1}. {step.strip('.')}" for i, step in enumerate(steps) if step.strip()]
139
- return "\n".join(steps)
 
 
 
 
 
 
140
 
141
  # Knowledge Base Search Interface
142
  demo1 = gr.Interface(
@@ -159,14 +183,14 @@ demo2 = gr.Interface(
159
  # Response Formatter Interface
160
  demo3 = gr.Interface(
161
  fn=format_response,
162
- inputs=[gr.Textbox(label="Raw Steps", placeholder="Enter steps separated by periods")],
163
  outputs=[gr.Textbox(label="Formatted Steps")],
164
  title="Response Formatter",
165
- description="Enter raw steps separated by periods to format them as a numbered list."
166
  )
167
 
168
  # Combine all interfaces using Tabs
169
  demo = gr.TabbedInterface([demo1, demo2, demo3], ["Knowledge Base", "Web Search", "Formatter"])
170
 
171
  if __name__ == "__main__":
172
- demo.launch() # Added share=True for public access
 
4
  import json
5
  from urllib.parse import quote_plus
6
  import time
7
+ import logging
8
+
9
+ # Configure logging
10
+ logging.basicConfig(level=logging.INFO)
11
+ logger = logging.getLogger(__name__)
12
 
13
  def search_knowledge_base(issue):
14
  """
 
18
  Returns:
19
  str: Solution steps or message if no solution found
20
  """
21
+ try:
22
+ issue = issue.lower()
23
+ if "wifi" in issue:
24
+ return "1. Check if your router is powered on\n2. Restart your router\n3. Try connecting again\n4. If problem persists, contact your ISP"
25
+ elif "screen" in issue:
26
+ return "1. Adjust display cable connections\n2. Update graphics drivers\n3. Restart the system\n4. Try a different monitor if available"
27
+ elif "sound" in issue or "audio" in issue:
28
+ return "1. Check volume settings\n2. Verify audio output device selection\n3. Update audio drivers\n4. Test with headphones"
29
+ return "No predefined solution found in the knowledge base. Please try our web search tool for more information."
30
+ except Exception as e:
31
+ logger.error(f"Error in knowledge base search: {str(e)}")
32
+ return f"Error searching knowledge base: {str(e)}"
33
 
34
  def search_web_duckduckgo(query):
35
  """
 
71
  return f"Search completed for '{query}' but no detailed results available. Try a more specific query."
72
 
73
  except requests.RequestException as e:
74
+ logger.error(f"Web search API error: {str(e)}")
75
  return f"Error performing web search: {str(e)}"
76
  except Exception as e:
77
+ logger.error(f"Unexpected web search error: {str(e)}")
78
  return f"Unexpected error: {str(e)}"
79
 
80
  def search_web_scraper(query):
 
113
  return f"No results found for '{query}'. Try different keywords."
114
 
115
  except Exception as e:
116
+ logger.error(f"Web scraping error: {str(e)}")
117
  return f"Error in backup search: {str(e)}"
118
 
119
  def search_web(query):
 
124
  Returns:
125
  str: Formatted search results
126
  """
127
+ try:
128
+ if not query.strip():
129
+ return "Please enter a search query."
130
+
131
+ # Try DuckDuckGo API first
132
+ result = search_web_duckduckgo(query)
133
+
134
+ # If API fails, try scraping method
135
+ if "Error" in result:
136
+ logger.info("API method failed, trying scraping method...")
137
+ result = search_web_scraper(query)
138
+
139
+ return result
140
+ except Exception as e:
141
+ logger.error(f"Error in main web search: {str(e)}")
142
+ return f"Error processing your search: {str(e)}"
143
 
144
  def format_response(raw_steps):
145
  """
 
149
  Returns:
150
  str: Formatted numbered list of steps
151
  """
152
+ try:
153
+ if not raw_steps.strip():
154
+ return "Please enter some text to format."
155
+
156
+ # Handle multiple delimiters
157
+ steps = re.split(r'[.,;]\s*', raw_steps)
158
+ steps = [step.strip() for step in steps if step.strip()]
159
+ steps = [f"{i+1}. {step}" for i, step in enumerate(steps)]
160
+ return "\n".join(steps)
161
+ except Exception as e:
162
+ logger.error(f"Error formatting steps: {str(e)}")
163
+ return f"Error formatting your steps: {str(e)}"
164
 
165
  # Knowledge Base Search Interface
166
  demo1 = gr.Interface(
 
183
  # Response Formatter Interface
184
  demo3 = gr.Interface(
185
  fn=format_response,
186
+ inputs=[gr.Textbox(label="Raw Steps", placeholder="Enter steps separated by periods or semicolons")],
187
  outputs=[gr.Textbox(label="Formatted Steps")],
188
  title="Response Formatter",
189
+ description="Enter raw steps separated by periods or semicolons to format them as a numbered list."
190
  )
191
 
192
  # Combine all interfaces using Tabs
193
  demo = gr.TabbedInterface([demo1, demo2, demo3], ["Knowledge Base", "Web Search", "Formatter"])
194
 
195
  if __name__ == "__main__":
196
+ demo.launch()