Abhaykoul commited on
Commit
707a72b
1 Parent(s): 84a0b9e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +157 -0
app.py ADDED
@@ -0,0 +1,157 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import logging
2
+ import base64
3
+ import asyncio
4
+
5
+ from typing import Optional
6
+
7
+ from fastapi import FastAPI, Query
8
+
9
+ from webscout import WEBS
10
+
11
+ import requests
12
+ from requests.exceptions import RequestException
13
+ from random import randint
14
+
15
+ class Image:
16
+ """
17
+ This class provides methods for generating images based on prompts.
18
+ """
19
+
20
+ def create(self, prompt):
21
+ """
22
+ Create a new image generation based on the given prompt.
23
+
24
+ Args:
25
+ prompt (str): The prompt for generating the image.
26
+
27
+ Returns:
28
+ resp: The generated image content
29
+ """
30
+ headers = {
31
+ "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.5112.79 Safari/537.36",
32
+ }
33
+ try:
34
+ resp = requests.get(
35
+ "https://api.prodia.com/generate",
36
+ params={
37
+ "new": "true",
38
+ "prompt": prompt,
39
+ "model": "dreamshaper_6BakedVae.safetensors [114c8abb]",
40
+ "negative_prompt": "(nsfw:1.5),verybadimagenegative_v1.3, ng_deepnegative_v1_75t, (ugly face:0.5),cross-eyed,sketches, (worst quality:2), (low quality:2.1), (normal quality:2), lowres, normal quality, ((monochrome)), ((grayscale)), skin spots, acnes, skin blemishes, bad anatomy, DeepNegative, facing away, tilted head, {Multiple people}, lowres, bad anatomy, bad hands, text, error, missing fingers, extra digit, fewer digits, cropped, worstquality, low quality, normal quality, jpegartifacts, signature, watermark, username, blurry, bad feet, cropped, poorly drawn hands, poorly drawn face, mutation, deformed, worst quality, low quality, normal quality, jpeg artifacts, signature, watermark, extra fingers, fewer digits, extra limbs, extra arms,extra legs, malformed limbs, fused fingers, too many fingers, long neck, cross-eyed,mutated hands, polar lowres, bad body, bad proportions, gross proportions, text, error, missing fingers, missing arms, missing legs, extra digit, extra arms, extra leg, extra foot, repeating hair",
41
+ "steps": "50",
42
+ "cfg": "9.5",
43
+ "seed": randint(1, 10000),
44
+ "sampler": "Euler",
45
+ "aspect_ratio": "square",
46
+ },
47
+ headers=headers,
48
+ timeout=30,
49
+ )
50
+ data = resp.json()
51
+ while True:
52
+ resp = requests.get(f"https://api.prodia.com/job/{data['job']}", headers=headers)
53
+ json = resp.json()
54
+ if json["status"] == "succeeded":
55
+ return requests.get(
56
+ f"https://images.prodia.xyz/{data['job']}.png?download=1",
57
+ headers=headers,
58
+ ).content
59
+ except RequestException as exc:
60
+ raise RequestException("Unable to fetch the response.") from exc
61
+
62
+ imag = Image()
63
+ TIMEOUT = 10
64
+ PROXY = None
65
+
66
+ class SearchAPIApp:
67
+ def __init__(self):
68
+ self.app = FastAPI(
69
+ docs_url="/",
70
+ title="Webscout API",
71
+ swagger_ui_parameters={"defaultModelsExpandDepth": -1},
72
+ version="1.3",
73
+ )
74
+ self.setup_routes()
75
+
76
+ def setup_routes(self):
77
+ @self.app.get("/api/search")
78
+ async def search_text(
79
+ q: str = Query(..., description="The search query string"),
80
+ max_results: int = Query(10, ge=1, le=100, description="The maximum number of results to return"),
81
+ timelimit: Optional[str] = Query(None, description="The time limit for the search (e.g., 'd' for day, 'w' for week, 'm' for month, 'y' for year)"),
82
+ safesearch: str = Query("moderate", description="The safe search level ('on', 'moderate', or 'off')"),
83
+ region: str = Query("wt-wt", description="The region for the search (e.g., 'us-en', 'uk-en', 'ru-ru')"),
84
+ ):
85
+ """
86
+ Perform a text search using the Webscout API.
87
+ """
88
+ WEBS_instance = WEBS()
89
+ results = []
90
+ # Assuming WEBS_instance.text returns a list directly
91
+ results = WEBS_instance.text(q, max_results=max_results, timelimit=timelimit, safesearch=safesearch, region=region)
92
+
93
+ return {"results": results}
94
+
95
+ @self.app.get("/api/images")
96
+ async def search_images(
97
+ q: str = Query(..., description="The search query string"),
98
+ max_results: int = Query(10, ge=1, le=100, description="The maximum number of results to return"),
99
+ safesearch: str = Query("moderate", description="The safe search level ('on', 'moderate', or 'off')"),
100
+ region: str = Query("wt-wt", description="The region for the search (e.g., 'us-en', 'uk-en', 'ru-ru')"),
101
+ ):
102
+ """
103
+ Perform an image search using the Webscout API.
104
+ """
105
+ WEBS_instance = WEBS()
106
+ results = WEBS_instance.images(q, max_results=max_results, safesearch=safesearch, region=region)
107
+ return {"results": results}
108
+
109
+ @self.app.get("/api/videos")
110
+ async def search_videos(
111
+ q: str = Query(..., description="The search query string"),
112
+ max_results: int = Query(10, ge=1, le=100, description="The maximum number of results to return"),
113
+ safesearch: str = Query("moderate", description="The safe search level ('on', 'moderate', or 'off')"),
114
+ region: str = Query("wt-wt", description="The region for the search (e.g., 'us-en', 'uk-en', 'ru-ru')"),
115
+ timelimit: Optional[str] = Query(None, description="The time limit for the search (e.g., 'd' for day, 'w' for week, 'm' for month)"),
116
+ resolution: Optional[str] = Query(None, description="The resolution of the videos ('small', 'medium', 'large')"),
117
+ duration: Optional[str] = Query(None, description="The duration of the videos ('short', 'medium', 'long')"),
118
+ ):
119
+ """
120
+ Perform a video search using the Webscout API.
121
+ """
122
+ WEBS_instance = WEBS()
123
+ results = WEBS_instance.videos(q, max_results=max_results, safesearch=safesearch, region=region, timelimit=timelimit, resolution=resolution, duration=duration)
124
+ return {"results": results}
125
+
126
+ @self.app.get("/api/news")
127
+ async def search_news(
128
+ q: str = Query(..., description="The search query string"),
129
+ max_results: int = Query(10, ge=1, le=100, description="The maximum number of results to return"),
130
+ safesearch: str = Query("moderate", description="The safe search level ('on', 'moderate', or 'off')"),
131
+ region: str = Query("wt-wt", description="The region for the search (e.g., 'us-en', 'uk-en', 'ru-ru')"),
132
+ timelimit: Optional[str] = Query(None, description="The time limit for the search (e.g., 'd' for day, 'w' for week, 'm' for month)"),
133
+ ):
134
+ """
135
+ Perform a news search using the Webscout API.
136
+ """
137
+ WEBS_instance = WEBS()
138
+ results = WEBS_instance.news(q, max_results=max_results, safesearch=safesearch, region=region, timelimit=timelimit)
139
+ return {"results": results}
140
+
141
+
142
+ @self.app.get("/api/image/gen")
143
+ async def generate_image(
144
+ prompt: str = Query(..., description="The prompt to generate an image from")
145
+ ):
146
+ """
147
+ Generate an image using the Image class.
148
+ """
149
+ try:
150
+ image_content = imag.create(prompt)
151
+ image_base64 = base64.b64encode(image_content).decode('utf-8')
152
+ return {"image": image_base64}
153
+ except Exception as e:
154
+ return {"error": str(e)}, 500
155
+
156
+
157
+ app = SearchAPIApp().app