Navanjana
commited on
Commit
•
b293841
1
Parent(s):
53f5b12
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,122 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
import json
|
3 |
+
import wikipedia
|
4 |
+
import requests
|
5 |
+
from bs4 import BeautifulSoup
|
6 |
+
import gradio as gr
|
7 |
+
from transformers import pipeline
|
8 |
+
|
9 |
+
# Set up Google SERP API credentials
|
10 |
+
serp_api_key = '5924c6cfe5fec240e39838ff06439c8d36d294a0' # Replace with your actual Google SERP API key
|
11 |
+
|
12 |
+
# Function to send a message and receive a response from the chatbot
|
13 |
+
def chat(message):
|
14 |
+
try:
|
15 |
+
# You can add your chatbot implementation here
|
16 |
+
return "This is a dummy chat response."
|
17 |
+
except Exception as e:
|
18 |
+
print("An error occurred:", e)
|
19 |
+
return ""
|
20 |
+
|
21 |
+
# Function to get the latest answers from Google SERP API
|
22 |
+
def get_latest_answers(query):
|
23 |
+
url = "https://google.serper.dev/search"
|
24 |
+
|
25 |
+
payload = json.dumps({
|
26 |
+
"q": query
|
27 |
+
})
|
28 |
+
headers = {
|
29 |
+
'X-API-KEY': serp_api_key,
|
30 |
+
'Content-Type': 'application/json'
|
31 |
+
}
|
32 |
+
|
33 |
+
response = requests.request("POST", url, headers=headers, data=payload)
|
34 |
+
|
35 |
+
try:
|
36 |
+
# Parse the response JSON
|
37 |
+
data = json.loads(response.text)
|
38 |
+
|
39 |
+
# Extract details from the response
|
40 |
+
output = ""
|
41 |
+
|
42 |
+
if 'knowledgeGraph' in data:
|
43 |
+
knowledge_graph = data['knowledgeGraph']
|
44 |
+
output += "Website: {}\n".format(knowledge_graph.get('website'))
|
45 |
+
output += "Description: {}\n".format(knowledge_graph.get('description'))
|
46 |
+
|
47 |
+
if 'organic' in data:
|
48 |
+
organic_results = data['organic']
|
49 |
+
for result in organic_results:
|
50 |
+
output += "Snippet: {}\n".format(result.get('snippet'))
|
51 |
+
|
52 |
+
if 'peopleAlsoAsk' in data:
|
53 |
+
people_also_ask = data['peopleAlsoAsk']
|
54 |
+
for question in people_also_ask:
|
55 |
+
output += "Snippet: {}\n".format(question.get('snippet'))
|
56 |
+
|
57 |
+
return output
|
58 |
+
|
59 |
+
except json.JSONDecodeError:
|
60 |
+
print(".")
|
61 |
+
return ""
|
62 |
+
|
63 |
+
except Exception as e:
|
64 |
+
print(".")
|
65 |
+
return ""
|
66 |
+
|
67 |
+
# Function to search Wikipedia for an answer and summarize it
|
68 |
+
def search_wikipedia(query):
|
69 |
+
try:
|
70 |
+
search_results = wikipedia.search(query)
|
71 |
+
|
72 |
+
# Get the page summary of the first search result
|
73 |
+
if search_results:
|
74 |
+
page_title = search_results[0]
|
75 |
+
page_summary = wikipedia.summary(page_title)
|
76 |
+
return page_summary
|
77 |
+
else:
|
78 |
+
print(".")
|
79 |
+
return None
|
80 |
+
except wikipedia.exceptions.DisambiguationError as e:
|
81 |
+
# Handle disambiguation error
|
82 |
+
print(".")
|
83 |
+
return None
|
84 |
+
except wikipedia.exceptions.PageError as e:
|
85 |
+
# Handle page not found error
|
86 |
+
print(".")
|
87 |
+
return None
|
88 |
+
except Exception as e:
|
89 |
+
# Handle other exceptions
|
90 |
+
print(".")
|
91 |
+
return None
|
92 |
+
|
93 |
+
# Function to generate summarized paragraph using transformer-based summarization
|
94 |
+
def generate_summary(user_input):
|
95 |
+
output = get_latest_answers(user_input)
|
96 |
+
page_summary = search_wikipedia(user_input)
|
97 |
+
chat_answer = chat(user_input)
|
98 |
+
|
99 |
+
# Generate summarized paragraph using transformer-based summarization
|
100 |
+
summarizer = pipeline("summarization")
|
101 |
+
input_text = f"\n{output}\n{page_summary}\n"
|
102 |
+
summarized_paragraph = summarizer(input_text, max_length=200, do_sample=True)[0]['summary_text']
|
103 |
+
|
104 |
+
return summarized_paragraph
|
105 |
+
|
106 |
+
# Define the Gradio interface
|
107 |
+
def summarizer_interface(user_input):
|
108 |
+
summarized_text = generate_summary(user_input)
|
109 |
+
return summarized_text
|
110 |
+
|
111 |
+
iface = gr.Interface(
|
112 |
+
fn=summarizer_interface,
|
113 |
+
inputs="text",
|
114 |
+
outputs="text",
|
115 |
+
title="Osana Web-GPT",
|
116 |
+
description="Enter your query and get the latest and better answer.",
|
117 |
+
theme="black",
|
118 |
+
layout="horizontal",
|
119 |
+
)
|
120 |
+
|
121 |
+
# Launch the interface
|
122 |
+
iface.launch()
|