Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio
|
| 2 |
+
import requests
|
| 3 |
+
import os
|
| 4 |
+
import json
|
| 5 |
+
from bardapi import Bard
|
| 6 |
+
|
| 7 |
+
bardKey = os.environ.get('_BARD_API_KEY')
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def bardChat(data):
|
| 11 |
+
# Create a session object using the requests library
|
| 12 |
+
session = requests.Session()
|
| 13 |
+
|
| 14 |
+
# Set the headers for the session
|
| 15 |
+
session.headers = {
|
| 16 |
+
"Host": "bard.google.com",
|
| 17 |
+
"X-Same-Domain": "1",
|
| 18 |
+
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36",
|
| 19 |
+
"Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
|
| 20 |
+
"Origin": "https://bard.google.com",
|
| 21 |
+
"Referer": "https://bard.google.com/",
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
# Set the "__Secure-1PSID" cookie with the Bard API key
|
| 25 |
+
session.cookies.set("__Secure-1PSID", bardKey)
|
| 26 |
+
|
| 27 |
+
# Create a Bard object with the session and a timeout of 30 seconds
|
| 28 |
+
bard = Bard(session=session, timeout=30)
|
| 29 |
+
answer = bard.get_answer(data)['content']
|
| 30 |
+
print(answer)
|
| 31 |
+
return json.dumps({'message':answer,'action':'null'})
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def responsenew(data):
|
| 35 |
+
return bardChat(data)
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
gradio_interface = gradio.Interface(
|
| 39 |
+
fn = responsenew,
|
| 40 |
+
inputs = "text",
|
| 41 |
+
outputs = "text"
|
| 42 |
+
)
|
| 43 |
+
gradio_interface.launch()
|