aka7774 commited on
Commit
2bdbf4a
·
verified ·
1 Parent(s): 55407e8

Upload 6 files

Browse files
Files changed (6) hide show
  1. app.py +6 -32
  2. fn.py +36 -0
  3. install.bat +56 -0
  4. main.py +58 -0
  5. requirements.txt +3 -0
  6. venv.sh +7 -0
app.py CHANGED
@@ -1,36 +1,10 @@
1
- import os
2
  import gradio as gr
3
 
4
- from duckduckgo_search import DDGS
5
- from urllib.request import urlopen
6
- from bs4 import BeautifulSoup
7
-
8
- def fn(text):
9
- with DDGS() as ddgs:
10
- results = [r for r in ddgs.text(text, max_results=5)]
11
-
12
- url = results[0]['href']
13
-
14
- html = urlopen(url).read()
15
- soup = BeautifulSoup(html, features="html.parser")
16
-
17
- # kill all script and style elements
18
- for script in soup(["script", "style"]):
19
- script.extract() # rip it out
20
-
21
- # get text
22
- text = soup.get_text()
23
-
24
- # break into lines and remove leading and trailing space on each
25
- lines = (line.strip() for line in text.splitlines())
26
- # break multi-headlines into a line each
27
- chunks = (phrase.strip() for line in lines for phrase in line.split(" "))
28
- # drop blank lines
29
- text = '\n'.join(chunk for chunk in chunks if chunk)
30
-
31
- return text, results
32
-
33
- gr.Interface(
34
- fn=fn,
35
  inputs="text",
36
  outputs=["text","text"]).launch()
 
 
 
 
1
+ import fn
2
  import gradio as gr
3
 
4
+ demo = gr.Interface(
5
+ fn=fn.run,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  inputs="text",
7
  outputs=["text","text"]).launch()
8
+
9
+ if __name__ == '__main__':
10
+ demo.launch()
fn.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+
3
+ from duckduckgo_search import DDGS
4
+ from urllib.request import urlopen
5
+ from bs4 import BeautifulSoup
6
+
7
+ def run(text):
8
+ results = ddg(text)
9
+ url = results[0]['href']
10
+ text = bs4(url)
11
+ return text, results
12
+
13
+ def ddg(text, max_results = 5):
14
+ with DDGS() as ddgs:
15
+ results = [r for r in ddgs.text(text, max_results=max_results)]
16
+ return results
17
+
18
+ def bs4(url):
19
+ html = urlopen(url).read()
20
+ soup = BeautifulSoup(html, features="html.parser")
21
+
22
+ # kill all script and style elements
23
+ for script in soup(["script", "style"]):
24
+ script.extract() # rip it out
25
+
26
+ # get text
27
+ text = soup.get_text()
28
+
29
+ # break into lines and remove leading and trailing space on each
30
+ lines = (line.strip() for line in text.splitlines())
31
+ # break multi-headlines into a line each
32
+ chunks = (phrase.strip() for line in lines for phrase in line.split(" "))
33
+ # drop blank lines
34
+ text = '\n'.join(chunk for chunk in chunks if chunk)
35
+
36
+ return text
install.bat ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ @echo off
2
+
3
+ rem -------------------------------------------
4
+ rem NOT guaranteed to work on Windows
5
+
6
+ set REPOS=https://huggingface.co/spaces/aka7774/ddg_bs4
7
+ set APPDIR=ddg_bs4
8
+ set VENV=venv
9
+
10
+ rem -------------------------------------------
11
+
12
+ set INSTALL_DIR=%~dp0
13
+ cd /d %INSTALL_DIR%
14
+
15
+ :git_clone
16
+ set DL_URL=%REPOS%
17
+ set DL_DST=%APPDIR%
18
+ git clone %DL_URL% %APPDIR%
19
+ if exist %DL_DST% goto install_python
20
+
21
+ set DL_URL=https://github.com/git-for-windows/git/releases/download/v2.41.0.windows.3/PortableGit-2.41.0.3-64-bit.7z.exe
22
+ set DL_DST=PortableGit-2.41.0.3-64-bit.7z.exe
23
+ curl -L -o %DL_DST% %DL_URL%
24
+ if not exist %DL_DST% bitsadmin /transfer dl %DL_URL% %DL_DST%
25
+ %DL_DST% -y
26
+ del %DL_DST%
27
+
28
+ set GIT=%INSTALL_DIR%PortableGit\bin\git
29
+ %GIT% clone %REPOS%
30
+
31
+ :install_python
32
+ set DL_URL=https://github.com/indygreg/python-build-standalone/releases/download/20240107/cpython-3.10.13+20240107-i686-pc-windows-msvc-shared-install_only.tar.gz
33
+ set DL_DST="%INSTALL_DIR%python.tar.gz"
34
+ curl -L -o %DL_DST% %DL_URL%
35
+ if not exist %DL_DST% bitsadmin /transfer dl %DL_URL% %DL_DST%
36
+ tar -xzf %DL_DST%
37
+
38
+ set PYTHON=%INSTALL_DIR%python\python.exe
39
+ set PATH=%PATH%;%INSTALL_DIR%python310\Scripts
40
+
41
+ :install_venv
42
+ cd %APPDIR%
43
+ %PYTHON% -m venv %VENV%
44
+ set PYTHON=%VENV%\Scripts\python.exe
45
+
46
+ :install_pip
47
+ set DL_URL=https://bootstrap.pypa.io/get-pip.py
48
+ set DL_DST=%INSTALL_DIR%get-pip.py
49
+ curl -o %DL_DST% %DL_URL%
50
+ if not exist %DL_DST% bitsadmin /transfer dl %DL_URL% %DL_DST%
51
+ %PYTHON% %DL_DST%
52
+
53
+ %PYTHON% -m pip install gradio
54
+ %PYTHON% -m pip install -r requirements.txt
55
+
56
+ pause
main.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import sys
3
+ import time
4
+ import signal
5
+ import psutil
6
+ import io
7
+
8
+ from fastapi import FastAPI, Request, status, Form, UploadFile
9
+ from fastapi.staticfiles import StaticFiles
10
+ from fastapi.middleware.cors import CORSMiddleware
11
+ from pydantic import BaseModel, Field
12
+ from fastapi.exceptions import RequestValidationError
13
+ from fastapi.responses import JSONResponse
14
+
15
+ import fn
16
+ import gradio as gr
17
+ from app import demo
18
+
19
+ app = FastAPI()
20
+
21
+ app.add_middleware(
22
+ CORSMiddleware,
23
+ allow_origins=['*'],
24
+ allow_credentials=True,
25
+ allow_methods=["*"],
26
+ allow_headers=["*"],
27
+ )
28
+
29
+ gr.mount_gradio_app(app, demo, path="/gradio")
30
+
31
+ fn.load_model()
32
+
33
+ @app.post("/run")
34
+ async def api_run(text: str):
35
+ try:
36
+ text, results = fn.run(text)
37
+
38
+ return {"text": text, "results": results}
39
+ except Exception as e:
40
+ return {"error": str(e)}
41
+
42
+ @app.post("/ddg")
43
+ async def api_ddg(text: str):
44
+ try:
45
+ results = fn.ddg(text)
46
+
47
+ return {"results": results}
48
+ except Exception as e:
49
+ return {"error": str(e)}
50
+
51
+ @app.post("/bs4")
52
+ async def api_bs4(url: str):
53
+ try:
54
+ text = fn.bs4(url)
55
+
56
+ return {"text": text}
57
+ except Exception as e:
58
+ return {"error": str(e)}
requirements.txt CHANGED
@@ -1,2 +1,5 @@
 
 
1
  duckduckgo_search
2
  beautifulsoup4
 
 
1
+ fastapi
2
+ uvicorn
3
  duckduckgo_search
4
  beautifulsoup4
5
+ python-multipart
venv.sh ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/bash
2
+
3
+ python3 -m venv venv
4
+ curl -kL https://bootstrap.pypa.io/get-pip.py | venv/bin/python
5
+
6
+ venv/bin/python -m pip install gradio
7
+ venv/bin/python -m pip install -r requirements.txt