Update app.py
Browse files
app.py
CHANGED
|
@@ -1,30 +1,33 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
| 3 |
|
| 4 |
-
# 1. Load the
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
-
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
# Extract the text properly
|
| 15 |
-
answer = result[0]['generated_text']
|
| 16 |
-
|
| 17 |
-
# Return the answer to the user
|
| 18 |
-
return answer
|
| 19 |
-
|
| 20 |
-
# 3. Create the App Interface
|
| 21 |
-
iface = gr.Interface(
|
| 22 |
-
fn=ask_brain,
|
| 23 |
-
inputs="text",
|
| 24 |
-
outputs="text",
|
| 25 |
-
title="My Free AI Model",
|
| 26 |
-
description="Ask me anything!"
|
| 27 |
)
|
| 28 |
|
| 29 |
-
#
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from llama_cpp import Llama
|
| 3 |
|
| 4 |
+
# 1. Load the Model
|
| 5 |
+
# This automatically downloads the "DeepSeek-R1-Distill-Llama-8B" (GGUF version)
|
| 6 |
+
# We use the Q4_K_M version because it fits in the FREE 16GB RAM tier.
|
| 7 |
+
print("⏳ Downloading & Loading Model... (This takes 1-2 mins on first run)")
|
| 8 |
|
| 9 |
+
llm = Llama.from_pretrained(
|
| 10 |
+
repo_id="bartowski/DeepSeek-R1-Distill-Llama-8B-GGUF",
|
| 11 |
+
filename="DeepSeek-R1-Distill-Llama-8B-Q4_K_M.gguf",
|
| 12 |
+
verbose=True,
|
| 13 |
+
n_ctx=4096 # Context window (memory of the conversation)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
)
|
| 15 |
|
| 16 |
+
# 2. Define the Chat Function
|
| 17 |
+
def chat_with_deepseek(message, history):
|
| 18 |
+
# Format the prompt for DeepSeek
|
| 19 |
+
# It expects: User: <msg> \n Assistant:
|
| 20 |
+
prompt = f"User: {message}\nAssistant:"
|
| 21 |
+
|
| 22 |
+
# Generate response
|
| 23 |
+
output = llm(
|
| 24 |
+
prompt,
|
| 25 |
+
max_tokens=512, # How long the answer can be
|
| 26 |
+
stop=["User:", "\n\n"], # Stop it from talking to itself
|
| 27 |
+
echo=False
|
| 28 |
+
)
|
| 29 |
+
return output['choices'][0]['text']
|
| 30 |
+
|
| 31 |
+
# 3. Launch the Chat Interface
|
| 32 |
+
# We use ChatInterface because it handles the UI automatically
|
| 33 |
+
gr.ChatInterface(chat_with_deepseek).launch()
|