alexbakers commited on
Commit
f3060c4
·
verified ·
1 Parent(s): 070f364

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +139 -0
app.py ADDED
@@ -0,0 +1,139 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import time
3
+ import os
4
+
5
+ # Set page title and description
6
+ st.set_page_config(
7
+ page_title="IPFS-LLaMA Demo",
8
+ page_icon="🦙",
9
+ layout="wide"
10
+ )
11
+
12
+ st.title("LLaMA-IPFS Demo")
13
+ st.markdown("This app demonstrates using llama-cpp-python with IPFS-hosted models.")
14
+
15
+ # Install required packages if not already installed
16
+ @st.cache_resource
17
+ def install_dependencies():
18
+ import subprocess
19
+ packages = ["llama-cpp-python", "huggingface-hub", "llama-ipfs"]
20
+ for package in packages:
21
+ try:
22
+ __import__(package)
23
+ except ImportError:
24
+ st.write(f"Installing {package}...")
25
+ subprocess.check_call(["pip", "install", package, "--quiet"])
26
+
27
+ # Activate IPFS integration
28
+ import llama_ipfs
29
+ llama_ipfs.activate()
30
+ st.success("✅ Dependencies installed and IPFS integration activated!")
31
+
32
+ # Display a loading spinner while installing dependencies
33
+ with st.spinner("Setting up environment..."):
34
+ install_dependencies()
35
+
36
+ # Load the LLaMA model
37
+ @st.cache_resource
38
+ def load_model(repo_id, filename):
39
+ from llama_cpp import Llama
40
+ with st.spinner(f"Loading model from {repo_id}/{filename}..."):
41
+ st.info("This may take a few minutes for the first run as the model is downloaded from IPFS.")
42
+ try:
43
+ model = Llama.from_pretrained(
44
+ repo_id=repo_id,
45
+ filename=filename,
46
+ verbose=False
47
+ )
48
+ return model
49
+ except Exception as e:
50
+ st.error(f"Error loading model: {str(e)}")
51
+ return None
52
+
53
+ # Model selection
54
+ model_options = {
55
+ "GPT-2 (117M)": {
56
+ "repo_id": "ipfs://bafybeie7quk74kmqg34nl2ewdwmsrlvvt6heayien364gtu2x6g2qpznhq",
57
+ "filename": "ggml-model-Q4_K_M.gguf"
58
+ }
59
+ }
60
+
61
+ # Sidebar for model selection and parameters
62
+ st.sidebar.title("Model Settings")
63
+
64
+ selected_model = st.sidebar.selectbox(
65
+ "Select Model",
66
+ list(model_options.keys()),
67
+ index=0
68
+ )
69
+
70
+ # Load the selected model
71
+ model_info = model_options[selected_model]
72
+ llm = load_model(model_info["repo_id"], model_info["filename"])
73
+
74
+ # Generation parameters
75
+ st.sidebar.title("Generation Settings")
76
+ temperature = st.sidebar.slider("Temperature", 0.0, 1.0, 0.0, 0.1)
77
+ max_tokens = st.sidebar.slider("Max Output Tokens", 10, 100, 30)
78
+ top_p = st.sidebar.slider("Top P", 0.1, 1.0, 0.95, 0.05)
79
+ repeat_penalty = st.sidebar.slider("Repeat Penalty", 1.0, 2.0, 1.0, 0.1)
80
+
81
+ # Main content area
82
+ st.header("Question & Answer")
83
+
84
+ # Input fields
85
+ context = st.text_area(
86
+ "Context",
87
+ value="France is a country in Western Europe known for its rich history and culture. "
88
+ "It is home to many famous landmarks including the Eiffel Tower. Its capital is Paris.",
89
+ height=150
90
+ )
91
+
92
+ question = st.text_input("Question", value="What is the capital of France?")
93
+
94
+ # Generate button
95
+ if st.button("Generate Answer"):
96
+ if llm is not None:
97
+ prompt = f"Context: {context}\nQuestion: {question}\nBased solely on the above context, answer the question in one word:"
98
+
99
+ # Show the prompt
100
+ with st.expander("Prompt"):
101
+ st.text(prompt)
102
+
103
+ # Generate the answer with progress
104
+ with st.spinner("Generating answer..."):
105
+ start_time = time.time()
106
+ output = llm(
107
+ prompt,
108
+ max_tokens=max_tokens,
109
+ temperature=temperature,
110
+ top_p=top_p,
111
+ repeat_penalty=repeat_penalty,
112
+ stop=["\n"]
113
+ )
114
+ end_time = time.time()
115
+
116
+ # Extract and display the answer
117
+ answer = output['choices'][0]['text'].strip()
118
+
119
+ st.success(f"Generated in {end_time - start_time:.2f} seconds")
120
+
121
+ # Display the answer
122
+ st.header("Answer")
123
+ st.write(answer)
124
+ else:
125
+ st.error("Model not loaded. Please check the error message above.")
126
+
127
+ # Information about the app
128
+ st.markdown("---")
129
+ st.markdown("""
130
+ ### How It Works
131
+
132
+ The `llama-ipfs` package patches the llama-cpp-python library to:
133
+
134
+ 1. Recognize `ipfs://` URIs as valid model identifiers
135
+ 2. Download model files from IPFS nodes or gateways
136
+ 3. Cache models locally for faster loading in subsequent runs
137
+
138
+ This allows you to load models from a decentralized network without changing any of your existing code!
139
+ """)