Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,28 +1,44 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
from llama_cpp import Llama
|
|
|
3 |
|
4 |
-
#
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
except ValueError as e:
|
9 |
-
print(f"Error loading model: {e}")
|
10 |
-
exit()
|
11 |
|
12 |
-
#
|
13 |
-
|
14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
return output["choices"][0]["text"]
|
16 |
|
17 |
-
#
|
18 |
iface = gr.Interface(
|
19 |
-
fn=
|
20 |
-
inputs="
|
21 |
outputs="text",
|
22 |
-
title="Phi-2 AI
|
23 |
-
description="Ask
|
24 |
)
|
25 |
|
26 |
-
# Launch the
|
27 |
if __name__ == "__main__":
|
28 |
iface.launch()
|
|
|
1 |
+
import os
|
2 |
import gradio as gr
|
3 |
from llama_cpp import Llama
|
4 |
+
import requests
|
5 |
|
6 |
+
# Define model details
|
7 |
+
MODEL_DIR = "/home/user/app/models/"
|
8 |
+
MODEL_PATH = MODEL_DIR + "phi-2.Q4_K_M.gguf"
|
9 |
+
MODEL_URL = "https://huggingface.co/TheBloke/Phi-2-GGUF/resolve/main/phi-2.Q4_K_M.gguf"
|
|
|
|
|
|
|
10 |
|
11 |
+
# Ensure the model directory exists
|
12 |
+
os.makedirs(MODEL_DIR, exist_ok=True)
|
13 |
+
|
14 |
+
# Download the model if not available
|
15 |
+
if not os.path.exists(MODEL_PATH):
|
16 |
+
print("Model not found! Downloading...")
|
17 |
+
response = requests.get(MODEL_URL, stream=True)
|
18 |
+
with open(MODEL_PATH, "wb") as f:
|
19 |
+
for chunk in response.iter_content(chunk_size=8192):
|
20 |
+
f.write(chunk)
|
21 |
+
print("Download complete!")
|
22 |
+
|
23 |
+
# Load the model
|
24 |
+
print("Loading model...")
|
25 |
+
model = Llama(model_path=MODEL_PATH)
|
26 |
+
print("Model loaded successfully!")
|
27 |
+
|
28 |
+
# Define function for Gradio interface
|
29 |
+
def chat_with_model(prompt):
|
30 |
+
output = model(prompt, max_tokens=256)
|
31 |
return output["choices"][0]["text"]
|
32 |
|
33 |
+
# Create Gradio interface
|
34 |
iface = gr.Interface(
|
35 |
+
fn=chat_with_model,
|
36 |
+
inputs=gr.Textbox(lines=2, placeholder="Ask your question here..."),
|
37 |
outputs="text",
|
38 |
+
title="Phi-2 AI Assistant",
|
39 |
+
description="Ask anything and get detailed responses!",
|
40 |
)
|
41 |
|
42 |
+
# Launch the interface
|
43 |
if __name__ == "__main__":
|
44 |
iface.launch()
|