Divyansh12 commited on
Commit
09e3194
1 Parent(s): 9518de8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -33
app.py CHANGED
@@ -1,38 +1,70 @@
1
  import streamlit as st
2
  from llama_cpp import Llama
3
- from huggingface_hub import snapshot_download
4
- import os
5
-
6
- st.title("GGUF Model Streamlit App")
7
-
8
- st.write("Downloading the model...")
9
- repo_id = "Divyansh12/check"
10
- model_dir = "model"
11
-
12
- # Download the model
13
- if not os.path.exists(model_dir):
14
- snapshot_download(repo_id=repo_id, cache_dir=model_dir)
15
- st.write("Model downloaded successfully!")
16
-
17
- # List downloaded files for debugging
18
- for root, dirs, files in os.walk(model_dir):
19
- st.write(f"Files in {root}: {files}")
20
-
21
- # Update this path based on the actual file structure found
22
- model_path = "model/models--Divyansh12--check/unsloth.F16.gguf"
23
- if not os.path.exists(model_path):
24
- st.error(f"Model file not found at {model_path}")
25
- else:
26
- st.write(f"Found model file at {model_path}")
27
- llm = Llama.from_pretrained(model_path=model_path)
28
- st.write("Model loaded successfully!")
29
-
30
- # Example query
31
- response = llm.create_chat_completion(
32
- messages=[
33
- {"role": "user", "content": "What is the capital of France?"}
34
- ]
35
  )
36
- st.write("Response:", response)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
 
38
 
 
1
  import streamlit as st
2
  from llama_cpp import Llama
3
+
4
+ # Initialize the model
5
+ llm = Llama.from_pretrained(
6
+ repo_id="Divyansh12/check",
7
+ filename="unsloth.F16.gguf", # Ensure this matches your model file name
8
+ verbose=True,
9
+ n_ctx=32768,
10
+ n_threads=2,
11
+ chat_format="chatml"
12
+ )
13
+
14
+ # Define the function to get responses from the model
15
+ def respond(message, history, system_message, max_tokens, temperature, top_p):
16
+ messages = [{"role": "system", "content": system_message}]
17
+
18
+ for user_message, assistant_message in history:
19
+ if user_message:
20
+ messages.append({"role": "user", "content": user_message})
21
+ if assistant_message:
22
+ messages.append({"role": "assistant", "content": assistant_message})
23
+
24
+ messages.append({"role": "user", "content": message})
25
+
26
+ response = ""
27
+ # Stream the response from the model
28
+ response_stream = llm.create_chat_completion(
29
+ messages=messages,
30
+ stream=True,
31
+ max_tokens=max_tokens,
32
+ temperature=temperature,
33
+ top_p=top_p
 
34
  )
35
+
36
+ for chunk in response_stream:
37
+ if len(chunk['choices'][0]["delta"]) != 0 and "content" in chunk['choices'][0]["delta"]:
38
+ response += chunk['choices'][0]["delta"]["content"]
39
+ yield response
40
+
41
+ # Streamlit UI
42
+ st.title("Chatbot Application")
43
+ st.write("### Interact with the chatbot!")
44
+
45
+ # User input fields
46
+ system_message = st.text_input("System Message", value="You are a friendly Chatbot.")
47
+ user_message = st.text_area("Your Message:")
48
+ max_tokens = st.slider("Max New Tokens", min_value=1, max_value=2048, value=512)
49
+ temperature = st.slider("Temperature", min_value=0.1, max_value=4.0, value=0.7)
50
+ top_p = st.slider("Top-p (Nucleus Sampling)", min_value=0.1, max_value=1.0, value=0.95)
51
+
52
+ # Chat history
53
+ if 'history' not in st.session_state:
54
+ st.session_state.history = []
55
+
56
+ if st.button("Send"):
57
+ # Get the response from the model
58
+ response = respond(user_message, st.session_state.history, system_message, max_tokens, temperature, top_p)
59
+
60
+ # Add user message and model response to history
61
+ st.session_state.history.append((user_message, response))
62
+
63
+ # Display the chat history
64
+ st.write("### Chat History")
65
+ for user_msg, assistant_msg in st.session_state.history:
66
+ st.write(f"**User:** {user_msg}")
67
+ st.write(f"**Assistant:** {assistant_msg}")
68
+
69
 
70