File size: 4,224 Bytes
f3060c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a3be4de
f3060c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import streamlit as st
import time
import os

# Set page title and description
st.set_page_config(
    page_title="IPFS-LLaMA Demo",
    page_icon="πŸ¦™",
    layout="wide"
)

st.title("LLaMA-IPFS Demo")
st.markdown("This app demonstrates using llama-cpp-python with IPFS-hosted models.")

# Install required packages if not already installed
@st.cache_resource
def install_dependencies():
    import subprocess
    packages = ["llama-cpp-python", "huggingface-hub", "llama-ipfs"]
    for package in packages:
        try:
            __import__(package)
        except ImportError:
            st.write(f"Installing {package}...")
            subprocess.check_call(["pip", "install", package, "--quiet"])
    
    # Activate IPFS integration
    import llama_ipfs
    llama_ipfs.activate()
    st.success("βœ… Dependencies installed and IPFS integration activated!")

# Display a loading spinner while installing dependencies
with st.spinner("Setting up environment..."):
    install_dependencies()

# Load the LLaMA model
@st.cache_resource
def load_model(repo_id, filename):
    from llama_cpp import Llama
    with st.spinner(f"Loading model from {repo_id}/{filename}..."):
        st.info("This may take a few minutes for the first run as the model is downloaded from IPFS.")
        try:
            model = Llama.from_pretrained(
                repo_id=repo_id, 
                filename=filename, 
                verbose=False
            )
            return model
        except Exception as e:
            st.error(f"Error loading model: {str(e)}")
            return None

# Model selection
model_options = {
    "GPT-2 (117M)": {
        "repo_id": "ipfs://bafybeie7quk74kmqg34nl2ewdwmsrlvvt6heayien364gtu2x6g2qpznhq",
        "filename": "ggml-model-Q4_K_M.gguf"
    }
}

# Sidebar for model selection and parameters
st.sidebar.title("Model Settings")

selected_model = st.sidebar.selectbox(
    "Select Model",
    list(model_options.keys()),
    index=0
)

# Load the selected model
model_info = model_options[selected_model]
llm = load_model(model_info["repo_id"], model_info["filename"])

# Generation parameters
st.sidebar.title("Generation Settings")
temperature = st.sidebar.slider("Temperature", 0.0, 1.0, 0.0, 0.1)
max_tokens = st.sidebar.slider("Max Output Tokens", 10, 100, 30)
top_p = st.sidebar.slider("Top P", 0.1, 1.0, 0.95, 0.05)
repeat_penalty = st.sidebar.slider("Repeat Penalty", 1.0, 2.0, 1.0, 0.1)

# Main content area
st.header("Question & Answer")

# Input fields
context = st.text_area(
    "Context",
    value="France is a country.",
    height=150
)

question = st.text_input("Question", value="What is the capital of France?")

# Generate button
if st.button("Generate Answer"):
    if llm is not None:
        prompt = f"Context: {context}\nQuestion: {question}\nBased solely on the above context, answer the question in one word:"
        
        # Show the prompt
        with st.expander("Prompt"):
            st.text(prompt)
        
        # Generate the answer with progress
        with st.spinner("Generating answer..."):
            start_time = time.time()
            output = llm(
                prompt,
                max_tokens=max_tokens,
                temperature=temperature,
                top_p=top_p,
                repeat_penalty=repeat_penalty,
                stop=["\n"]
            )
            end_time = time.time()
            
            # Extract and display the answer
            answer = output['choices'][0]['text'].strip()
            
            st.success(f"Generated in {end_time - start_time:.2f} seconds")
            
            # Display the answer
            st.header("Answer")
            st.write(answer)
    else:
        st.error("Model not loaded. Please check the error message above.")

# Information about the app
st.markdown("---")
st.markdown("""
### How It Works

The `llama-ipfs` package patches the llama-cpp-python library to:

1. Recognize `ipfs://` URIs as valid model identifiers
2. Download model files from IPFS nodes or gateways
3. Cache models locally for faster loading in subsequent runs

This allows you to load models from a decentralized network without changing any of your existing code!
""")