ntaexams commited on
Commit
df1b6e1
ยท
verified ยท
1 Parent(s): f5f2729

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -15
app.py CHANGED
@@ -1,30 +1,39 @@
1
- import os
2
- import gradio as gr
3
  import subprocess
 
4
  from llama_cpp import Llama
 
5
 
 
 
6
 
7
- # Model download link (Modify if needed)
8
- MODEL_URL = "https://huggingface.co/TheBloke/mistral-7b.Q4_K_M.gguf"
9
  MODEL_PATH = "./models/mistral-7b.Q4_K_M.gguf"
10
 
11
- # Create models directory if not exists
12
  os.makedirs("./models", exist_ok=True)
13
 
14
- # Auto-download model if not present
15
  if not os.path.exists(MODEL_PATH):
16
- print("Downloading Mistral-7B Q4 GGUF model...")
17
- subprocess.run(["wget", MODEL_URL, "-O", MODEL_PATH], check=True)
 
 
 
 
18
 
19
- # Load GGUF model
20
- print("Loading model...")
21
  model = Llama(model_path=MODEL_PATH, n_ctx=4096, n_threads=8)
 
22
 
23
- # Define function for chat
24
- def chat_with_ai(prompt):
25
- response = model(prompt, max_tokens=512, stop=["</s>"])
26
  return response["choices"][0]["text"]
27
 
28
- # Gradio UI
29
- iface = gr.Interface(fn=chat_with_ai, inputs="text", outputs="text", title="Mistral-7B GGUF Chatbot")
 
 
30
  iface.launch()
 
 
 
1
  import subprocess
2
+ import os
3
  from llama_cpp import Llama
4
+ import gradio as gr
5
 
6
+ # ๐Ÿ”น Get Hugging Face Token from environment variable
7
+ HF_TOKEN = os.getenv("HF_TOKEN")
8
 
9
+ # ๐Ÿ”น Model details
10
+ MODEL_URL = "https://huggingface.co/TheBloke/Mistral-7B-GGUF/resolve/main/mistral-7b.Q4_K_M.gguf"
11
  MODEL_PATH = "./models/mistral-7b.Q4_K_M.gguf"
12
 
13
+ # ๐Ÿ”น Ensure the models directory exists
14
  os.makedirs("./models", exist_ok=True)
15
 
16
+ # ๐Ÿ”น Check if the model exists, else download it
17
  if not os.path.exists(MODEL_PATH):
18
+ print("๐Ÿš€ Downloading Mistral-7B Q4 GGUF model...")
19
+ subprocess.run([
20
+ "wget", "--header", f"Authorization: Bearer {HF_TOKEN}",
21
+ MODEL_URL, "-O", MODEL_PATH
22
+ ], check=True)
23
+ print("โœ… Download complete!")
24
 
25
+ # ๐Ÿ”น Load the model
26
+ print("๐Ÿ“ฅ Loading the model...")
27
  model = Llama(model_path=MODEL_PATH, n_ctx=4096, n_threads=8)
28
+ print("โœ… Model loaded successfully!")
29
 
30
+ # ๐Ÿ”น Define a function to interact with the model
31
+ def chat_with_mistral(prompt):
32
+ response = model(prompt, max_tokens=512)
33
  return response["choices"][0]["text"]
34
 
35
+ # ๐Ÿ”น Create a Gradio UI
36
+ iface = gr.Interface(fn=chat_with_mistral, inputs="text", outputs="text", title="Mistral-7B Chatbot")
37
+
38
+ # ๐Ÿ”น Launch the app
39
  iface.launch()