Spaces:
Runtime error
Runtime error
v0.1
Browse files- README.md +6 -5
- app.py +67 -0
- requirements.txt +1 -0
README.md
CHANGED
@@ -1,13 +1,14 @@
|
|
|
|
1 |
---
|
2 |
-
title:
|
3 |
emoji: 🏢
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
6 |
sdk: gradio
|
7 |
sdk_version: 4.26.0
|
8 |
app_file: app.py
|
9 |
-
pinned:
|
10 |
license: mit
|
11 |
---
|
12 |
|
13 |
-
|
|
|
1 |
+
|
2 |
---
|
3 |
+
title: API Template - GGUF Edition
|
4 |
emoji: 🏢
|
5 |
+
colorFrom: yellow
|
6 |
+
colorTo: black
|
7 |
sdk: gradio
|
8 |
sdk_version: 4.26.0
|
9 |
app_file: app.py
|
10 |
+
pinned: true
|
11 |
license: mit
|
12 |
---
|
13 |
|
14 |
+
|
app.py
ADDED
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from llama_cpp import Llama
|
2 |
+
import gradio as gr
|
3 |
+
|
4 |
+
import os
|
5 |
+
import json
|
6 |
+
|
7 |
+
GRADIO_SHOW_API_INFO = os.getenv("AGS_SHOW_API_INFO") or True
|
8 |
+
|
9 |
+
AGS_REPO = os.getenv("AGS_REPO") or "lmstudio-community/gemma-1.1-2b-it-GGUF"
|
10 |
+
AGS_FILENAME = os.getenv("AGS_FILENAME") or "gemma-1.1-2b-it-Q4_K_M.gguf"
|
11 |
+
AGS_LLAMA_CONFIG = {
|
12 |
+
"prompt_format": "raw"
|
13 |
+
}
|
14 |
+
AGS_TITLE = os.getenv("AGS_TITLE") or "API GGUF Space"
|
15 |
+
|
16 |
+
try:
|
17 |
+
AGS_LLAMA_CONFIG = json.loads(os.getenv("AGS_LLAMA_CONFIG"))
|
18 |
+
except Exception as e:
|
19 |
+
if AGS_LLAMA_CONFIG and AGS_LLAMA_CONFIG is not None:
|
20 |
+
print("Invalid Llama config. Config must be valid JSON. Got:\n", AGS_LLAMA_CONFIG)
|
21 |
+
ARGS_LLAMA_CONFIG = {
|
22 |
+
"n_gpu_layers": 0,
|
23 |
+
}
|
24 |
+
|
25 |
+
def main():
|
26 |
+
llm = Llama.from_pretrained(AGS_REPO, filename=AGS_FILENAME, **ARGS_LLAMA_CONFIG)
|
27 |
+
def api_chat(inpt, settings):
|
28 |
+
res = None
|
29 |
+
try:
|
30 |
+
inpt = json.loads(inpt)
|
31 |
+
settings = json.loads(settings)
|
32 |
+
print("Request:\n" + json.dumps(inpt, indent=2) + "\n\n" + ("*_"*24))
|
33 |
+
except Exception as e:
|
34 |
+
res = llm(inpt, **settings)
|
35 |
+
if "@execute" in inpt and inpt['@execute']:
|
36 |
+
inpt.pop("@execute", None)
|
37 |
+
res = llm(json.dumps(inpt), **settings)
|
38 |
+
if "messages" in inpt and inpt['messages']:
|
39 |
+
res = llm.create_chat_completion(messages=inpt, **settings)
|
40 |
+
if "prompt" in settings and settings["prompt"]:
|
41 |
+
res = llm(inpt, **settings)
|
42 |
+
if res is None:
|
43 |
+
res = llm(json.dumps(inpt), **settings)
|
44 |
+
if settings and "full_output" in settings and settings["full_output"]:
|
45 |
+
return res
|
46 |
+
if "content" in res['choices'][0]:
|
47 |
+
return res['choices'][0]['content']
|
48 |
+
if "text" in res['choices'][0]:
|
49 |
+
return res['choices'][0]['text']
|
50 |
+
if "message" in res['choices'][0] and "content" in res['choices'][0]['message']:
|
51 |
+
return res['choices'][0]['message']['content']
|
52 |
+
return res
|
53 |
+
def chat(inpt):
|
54 |
+
if not inpt:
|
55 |
+
return ""
|
56 |
+
return llm.create_chat_completion(messages=[{
|
57 |
+
"role": "user",
|
58 |
+
"content": inpt
|
59 |
+
}])['choices'][0]['message']['content']
|
60 |
+
with gr.Interface(fn=chat, inputs=[inpt:=gr.Textbox()], outputs="text") as interface:
|
61 |
+
with gr.Row(visible=False):
|
62 |
+
shadow_input = gr.Textbox(visible=False)
|
63 |
+
shadow_input.submit(api_chat, inputs=[inpt, shadow_input], api_name="api_chat", server_name=AGS_TITLE)
|
64 |
+
interface.launch(debug=True, show_api=GRADIO_SHOW_API_INFO)
|
65 |
+
|
66 |
+
if __name__ == "__main__":
|
67 |
+
main()
|
requirements.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
llama-cpp-python
|