Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import time
|
| 2 |
+
from typing import Any, List, Mapping, Optional
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from langchain.callbacks.manager import CallbackManagerForLLMRun
|
| 5 |
+
from langchain.llms.base import LLM
|
| 6 |
+
import requests
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
class RunpodServerlessLLM(LLM):
|
| 10 |
+
pod_id: str
|
| 11 |
+
api_key: str
|
| 12 |
+
request_ids: List[str] = []
|
| 13 |
+
|
| 14 |
+
@property
|
| 15 |
+
def _llm_type(self) -> str:
|
| 16 |
+
return "runpod_serverless"
|
| 17 |
+
|
| 18 |
+
def _call(
|
| 19 |
+
self,
|
| 20 |
+
prompt: str,
|
| 21 |
+
stop: Optional[List[str]] = None,
|
| 22 |
+
run_manager: Optional[CallbackManagerForLLMRun] = None,
|
| 23 |
+
**kwargs: Any,
|
| 24 |
+
) -> str:
|
| 25 |
+
if stop is not None and self._current_job_id is not None:
|
| 26 |
+
#TODO: handle stop sequence
|
| 27 |
+
...
|
| 28 |
+
response = self._run_generate_request(prompt)
|
| 29 |
+
return response
|
| 30 |
+
|
| 31 |
+
@property
|
| 32 |
+
def _identifying_params(self) -> Mapping[str, Any]:
|
| 33 |
+
"""Get the identifying parameters."""
|
| 34 |
+
return {"pod_id": self.pod_id}
|
| 35 |
+
|
| 36 |
+
def _request_headers(self) -> Mapping[str, str]:
|
| 37 |
+
return {
|
| 38 |
+
"accept": "application/json",
|
| 39 |
+
"content-type": "application/json",
|
| 40 |
+
"authorization": self.api_key,
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
def _request_url(self) -> str:
|
| 44 |
+
return f"https://api.runpod.ai/v2/{self.pod_id}"
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
def _run_generate_request(self, prompt: str) -> str:
|
| 48 |
+
headers = self._request_headers()
|
| 49 |
+
input = {
|
| 50 |
+
"method_name": "generate",
|
| 51 |
+
"input": {"model": "mistral", "prompt": prompt},
|
| 52 |
+
}
|
| 53 |
+
print("before request", input, self._request_url(), headers)
|
| 54 |
+
|
| 55 |
+
# TODO: Handle network errors
|
| 56 |
+
out = requests.post(
|
| 57 |
+
f"{self._request_url()}/run",
|
| 58 |
+
headers=headers,
|
| 59 |
+
json={"input": input},
|
| 60 |
+
).json()
|
| 61 |
+
|
| 62 |
+
id = out["id"]
|
| 63 |
+
self.request_ids.append(id)
|
| 64 |
+
|
| 65 |
+
while out["status"] != "COMPLETED":
|
| 66 |
+
out = requests.get(
|
| 67 |
+
f"{self._request_url()}/status/{id}",
|
| 68 |
+
headers=headers,
|
| 69 |
+
).json()
|
| 70 |
+
time.sleep(1)
|
| 71 |
+
|
| 72 |
+
return out["output"]["response"]
|
| 73 |
+
|
| 74 |
+
llm = RunpodServerlessLLM(
|
| 75 |
+
pod_id="bbr5w1ja6f12mx",
|
| 76 |
+
api_key="B6WXXQ95XTG45909LKTQMPHP1F7JO09EXEMDTRN9",
|
| 77 |
+
)
|
| 78 |
+
def llmresponse(user_prompt):
|
| 79 |
+
response = llm.predict(user_prompt)
|
| 80 |
+
return response
|
| 81 |
+
|
| 82 |
+
iface = gr.Interface(
|
| 83 |
+
fn=llmresponse,
|
| 84 |
+
inputs=gr.Textbox(label="User Prompt", placeholder="Enter Your Prompt"),
|
| 85 |
+
outputs=gr.Textbox(label="LLM Output"),
|
| 86 |
+
title="Shady GPT😏",
|
| 87 |
+
description="No Censorship (Excluding Some Really Devilish shit) , Average Response Time - 45 seconds first try , Made By Akash Mondal"
|
| 88 |
+
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
iface.launch()
|