ntaexams commited on
Commit
bdd13cc
ยท
verified ยท
1 Parent(s): a825785

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -38
app.py CHANGED
@@ -1,39 +1,22 @@
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
- # ๐Ÿ”น Correct Model URL & Filename
10
- MODEL_URL = "https://huggingface.co/TheBloke/Mistral-7B-GGUF/resolve/main/mistral-7b-instruct-v0.1.Q4_K_M.gguf"
11
- MODEL_PATH = "./models/mistral-7b-instruct-v0.1.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()
 
 
1
  import os
2
+ from ctransformers import AutoModelForCausalLM
3
+
4
+ # Define the model repository and file
5
+ model_repo = "TheBloke/OpenHermes-2-Mistral-7B-GGUF"
6
+ model_file = "openhermes-2-mistral-7b.Q4_K_M.gguf"
7
+
8
+ # Download the model using ctransformers
9
+ print(f"Downloading {model_file} from {model_repo}...")
10
+ model = AutoModelForCausalLM.from_pretrained(
11
+ model_repo,
12
+ model_file=model_file,
13
+ model_type="mistral",
14
+ # Uncomment the following line if you have a CUDA-capable GPU
15
+ # gpu_layers=50
16
+ )
17
+ print("Model downloaded and loaded successfully.")
18
+
19
+ # Test the model with a simple prompt
20
+ prompt = "AI is going to"
21
+ response = model(prompt)
22
+ print(f"Prompt: {prompt}\nResponse: {response}")