Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from llama_cpp import Llama
|
| 3 |
+
from huggingface_hub import hf_hub_download
|
| 4 |
+
|
| 5 |
+
# Model download & load
|
| 6 |
+
MODEL_REPO = "dexcommunity/indexQ4"
|
| 7 |
+
MODEL_FILE = "indexq4.gguf"
|
| 8 |
+
model_path = hf_hub_download(repo_id=MODEL_REPO, filename=MODEL_FILE)
|
| 9 |
+
|
| 10 |
+
# Initialize Llama
|
| 11 |
+
llm = Llama(model_path=model_path, n_ctx=2048, n_threads=2)
|
| 12 |
+
|
| 13 |
+
def generate_api(prompt, max_tokens=256, temp=0.7):
|
| 14 |
+
# Gemma 2B Chat Template
|
| 15 |
+
formatted_prompt = f"<start_of_turn>user\n{prompt}<end_of_turn>\n<start_of_turn>model\n"
|
| 16 |
+
|
| 17 |
+
output = llm(
|
| 18 |
+
formatted_prompt,
|
| 19 |
+
max_tokens=int(max_tokens),
|
| 20 |
+
temperature=float(temp),
|
| 21 |
+
stop=["<end_of_turn>"],
|
| 22 |
+
echo=False
|
| 23 |
+
)
|
| 24 |
+
return output["choices"][0]["text"].strip()
|
| 25 |
+
|
| 26 |
+
# Gradio interface (API only mode)
|
| 27 |
+
demo = gr.Interface(
|
| 28 |
+
fn=generate_api,
|
| 29 |
+
inputs=[gr.Textbox(label="Prompt"), gr.Slider(1, 512, 256), gr.Slider(0, 1, 0.7)],
|
| 30 |
+
outputs=gr.Textbox(label="Reply")
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
if __name__ == "__main__":
|
| 34 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|