Spaces:
Sleeping
Sleeping
Navanjana
commited on
Commit
•
81d46e4
1
Parent(s):
775b462
Update app.py
Browse files
app.py
CHANGED
@@ -2,9 +2,10 @@ import openai
|
|
2 |
import requests
|
3 |
import json
|
4 |
import wikipedia
|
5 |
-
import requests
|
6 |
from bs4 import BeautifulSoup
|
7 |
import gradio as gr
|
|
|
|
|
8 |
|
9 |
# Set up the OpenAI API client
|
10 |
openai.api_key = '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' # Replace with your actual API key
|
@@ -12,8 +13,11 @@ openai.api_key = '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' # Replac
|
|
12 |
# Set up Google SERP API credentials
|
13 |
serp_api_key = '03c74289238ba82d2889379e7a958a07b56c45de' # Replace with your actual Google SERP API key
|
14 |
|
|
|
|
|
|
|
15 |
# Function to send a message and receive a response from the chatbot
|
16 |
-
def chat(message):
|
17 |
try:
|
18 |
response = openai.Completion.create(
|
19 |
engine='text-davinci-003', # Choose the language model/engine you want to use
|
@@ -22,7 +26,9 @@ def chat(message):
|
|
22 |
n=1, # Number of responses to generate
|
23 |
stop=None, # Specify a stop token to end the response
|
24 |
)
|
25 |
-
|
|
|
|
|
26 |
except Exception as e:
|
27 |
print("An error occurred:", e)
|
28 |
return ""
|
@@ -82,7 +88,11 @@ def search_wikipedia(query):
|
|
82 |
if search_results:
|
83 |
page_title = search_results[0]
|
84 |
page_summary = wikipedia.summary(page_title)
|
85 |
-
|
|
|
|
|
|
|
|
|
86 |
else:
|
87 |
print(".")
|
88 |
return None
|
@@ -103,7 +113,15 @@ def search_wikipedia(query):
|
|
103 |
def generate_summary(user_input):
|
104 |
output = get_latest_answers(user_input)
|
105 |
page_summary = search_wikipedia(user_input)
|
106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
107 |
|
108 |
# Generate summarized paragraph using OpenAI API
|
109 |
response = openai.Completion.create(
|
@@ -115,9 +133,27 @@ def generate_summary(user_input):
|
|
115 |
|
116 |
return summarized_paragraph
|
117 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
118 |
# Define the Gradio interface
|
119 |
def summarizer_interface(user_input):
|
120 |
-
|
|
|
|
|
|
|
121 |
return summarized_text
|
122 |
|
123 |
iface = gr.Interface(
|
|
|
2 |
import requests
|
3 |
import json
|
4 |
import wikipedia
|
|
|
5 |
from bs4 import BeautifulSoup
|
6 |
import gradio as gr
|
7 |
+
from googletrans import Translator
|
8 |
+
import langid
|
9 |
|
10 |
# Set up the OpenAI API client
|
11 |
openai.api_key = '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' # Replace with your actual API key
|
|
|
13 |
# Set up Google SERP API credentials
|
14 |
serp_api_key = '03c74289238ba82d2889379e7a958a07b56c45de' # Replace with your actual Google SERP API key
|
15 |
|
16 |
+
# Set up Google Translate client
|
17 |
+
translator = Translator(service_urls=['translate.google.com'])
|
18 |
+
|
19 |
# Function to send a message and receive a response from the chatbot
|
20 |
+
def chat(message, target_lang):
|
21 |
try:
|
22 |
response = openai.Completion.create(
|
23 |
engine='text-davinci-003', # Choose the language model/engine you want to use
|
|
|
26 |
n=1, # Number of responses to generate
|
27 |
stop=None, # Specify a stop token to end the response
|
28 |
)
|
29 |
+
response_text = response.choices[0].text.strip()
|
30 |
+
translated_text = translator.translate(response_text, dest=target_lang).text
|
31 |
+
return translated_text
|
32 |
except Exception as e:
|
33 |
print("An error occurred:", e)
|
34 |
return ""
|
|
|
88 |
if search_results:
|
89 |
page_title = search_results[0]
|
90 |
page_summary = wikipedia.summary(page_title)
|
91 |
+
|
92 |
+
# Translate the summary to English
|
93 |
+
page_summary_en = translator.translate(page_summary, dest='en').text
|
94 |
+
|
95 |
+
return page_summary_en
|
96 |
else:
|
97 |
print(".")
|
98 |
return None
|
|
|
113 |
def generate_summary(user_input):
|
114 |
output = get_latest_answers(user_input)
|
115 |
page_summary = search_wikipedia(user_input)
|
116 |
+
|
117 |
+
# Translate the user input to English
|
118 |
+
user_input_en = translator.translate(user_input, dest='en').text
|
119 |
+
|
120 |
+
chat_answer_en = chat(user_input_en, 'en')
|
121 |
+
|
122 |
+
# Translate the chatbot's response back to the detected input language
|
123 |
+
detected_lang = langid.classify(user_input)[0]
|
124 |
+
chat_answer = translator.translate(chat_answer_en, dest=detected_lang).text
|
125 |
|
126 |
# Generate summarized paragraph using OpenAI API
|
127 |
response = openai.Completion.create(
|
|
|
133 |
|
134 |
return summarized_paragraph
|
135 |
|
136 |
+
# Function to detect the input language
|
137 |
+
def detect_language(text):
|
138 |
+
detected_lang = langid.classify(text)[0]
|
139 |
+
return detected_lang
|
140 |
+
|
141 |
+
# Function to translate text to English
|
142 |
+
def translate_to_english(text):
|
143 |
+
translated_text = translator.translate(text, dest='en').text
|
144 |
+
return translated_text
|
145 |
+
|
146 |
+
# Function to translate text to the detected input language
|
147 |
+
def translate_to_input_language(text, input_lang):
|
148 |
+
translated_text = translator.translate(text, dest=input_lang).text
|
149 |
+
return translated_text
|
150 |
+
|
151 |
# Define the Gradio interface
|
152 |
def summarizer_interface(user_input):
|
153 |
+
input_lang = detect_language(user_input)
|
154 |
+
user_input_english = translate_to_english(user_input)
|
155 |
+
summarized_text_english = generate_summary(user_input_english)
|
156 |
+
summarized_text = translate_to_input_language(summarized_text_english, input_lang)
|
157 |
return summarized_text
|
158 |
|
159 |
iface = gr.Interface(
|