ntaexams commited on
Commit
992421f
·
verified ·
1 Parent(s): 84d5a05

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -16
app.py CHANGED
@@ -1,28 +1,44 @@
 
1
  import gradio as gr
2
  from llama_cpp import Llama
 
3
 
4
- # Load the model (Ensure the GGUF file is in the same directory)
5
- model = Llama(model_path="./phi-2.Q4_K_M.gguf") # Update path if needed
6
- try:
7
- model = Llama(model_path=model_path) # Load the quantized model
8
- except ValueError as e:
9
- print(f"Error loading model: {e}")
10
- exit()
11
 
12
- # Function to process user queries
13
- def solve_problem(prompt):
14
- output = model(prompt, max_tokens=512)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  return output["choices"][0]["text"]
16
 
17
- # Set up Gradio UI
18
  iface = gr.Interface(
19
- fn=solve_problem,
20
- inputs="text",
21
  outputs="text",
22
- title="Phi-2 AI Solver",
23
- description="Ask me any JEE/NEET-level questions!",
24
  )
25
 
26
- # Launch the app
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()