Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,51 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from llama_index.llms.huggingface import HuggingFaceLLM
|
| 2 |
|
|
|
|
| 3 |
def messages_to_prompt(messages):
|
| 4 |
prompt = ""
|
| 5 |
for message in messages:
|
| 6 |
if message.role == 'system':
|
| 7 |
-
prompt += f"
|
| 8 |
elif message.role == 'user':
|
| 9 |
-
prompt += f"
|
| 10 |
elif message.role == 'assistant':
|
| 11 |
-
prompt += f"
|
| 12 |
-
|
| 13 |
# ensure we start with a system prompt, insert blank if needed
|
| 14 |
-
if not prompt.startswith("
|
| 15 |
-
prompt = "
|
| 16 |
-
|
| 17 |
# add final assistant prompt
|
| 18 |
-
prompt = prompt + "
|
| 19 |
-
|
| 20 |
return prompt
|
| 21 |
|
|
|
|
| 22 |
def completion_to_prompt(completion):
|
| 23 |
-
return f"
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import BitsAndBytesConfig
|
| 4 |
from llama_index.llms.huggingface import HuggingFaceLLM
|
| 5 |
|
| 6 |
+
# Function to convert messages to prompt
|
| 7 |
def messages_to_prompt(messages):
|
| 8 |
prompt = ""
|
| 9 |
for message in messages:
|
| 10 |
if message.role == 'system':
|
| 11 |
+
prompt += f"\n{message.content}</s>\n"
|
| 12 |
elif message.role == 'user':
|
| 13 |
+
prompt += f"\n{message.content}</s>\n"
|
| 14 |
elif message.role == 'assistant':
|
| 15 |
+
prompt += f"\n{message.content}</s>\n"
|
| 16 |
+
|
| 17 |
# ensure we start with a system prompt, insert blank if needed
|
| 18 |
+
if not prompt.startswith("\n"):
|
| 19 |
+
prompt = "\n</s>\n" + prompt
|
| 20 |
+
|
| 21 |
# add final assistant prompt
|
| 22 |
+
prompt = prompt + "\n"
|
| 23 |
+
|
| 24 |
return prompt
|
| 25 |
|
| 26 |
+
# Function to convert completion to prompt
|
| 27 |
def completion_to_prompt(completion):
|
| 28 |
+
return f"\n</s>\n\n{completion}</s>\n\n"
|
| 29 |
+
|
| 30 |
+
# Load the LLM without quantization
|
| 31 |
+
@st.cache_resource
|
| 32 |
+
def load_llm():
|
| 33 |
+
return HuggingFaceLLM(
|
| 34 |
+
model_name="HuggingFaceH4/zephyr-7b-beta",
|
| 35 |
+
tokenizer_name="HuggingFaceH4/zephyr-7b-beta",
|
| 36 |
+
context_window=3900,
|
| 37 |
+
max_new_tokens=256,
|
| 38 |
+
generate_kwargs={"temperature": 0.7, "top_k": 50, "top_p": 0.95},
|
| 39 |
+
messages_to_prompt=messages_to_prompt,
|
| 40 |
+
completion_to_prompt=completion_to_prompt,
|
| 41 |
+
device_map="cpu" # Use CPU
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
llm = load_llm()
|
| 45 |
+
|
| 46 |
+
# Streamlit app interface
|
| 47 |
+
st.title("LLM Text Generation App")
|
| 48 |
+
|
| 49 |
+
# Text input for the prompt
|
| 50 |
+
user_input = st.text_area("Enter your prompt:", "")
|
| 51 |
+
|
| 52 |
+
# Button to generate response
|
| 53 |
+
if st.button("Generate Response"):
|
| 54 |
+
if user_input.strip() != "":
|
| 55 |
+
# Generate response based on the prompt
|
| 56 |
+
with st.spinner("Generating response..."):
|
| 57 |
+
response = llm.complete(user_input)
|
| 58 |
+
|
| 59 |
+
# Display the generated response
|
| 60 |
+
st.write("Generated Response:")
|
| 61 |
+
st.write(str(response))
|
| 62 |
+
else:
|
| 63 |
+
st.warning("Please enter a valid prompt.")
|