Update app.py
Browse files
app.py
CHANGED
|
@@ -1,58 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
-
|
| 3 |
-
from
|
| 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 |
else:
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
|
| 3 |
+
import gradio as gr
|
| 4 |
import streamlit as st
|
| 5 |
+
from transformers import pipeline
|
| 6 |
+
from datasets import load_dataset
|
| 7 |
+
|
| 8 |
+
# Initialize text-generation pipeline with the model
|
| 9 |
+
model_name = "nvidia/Llama-3.1-Nemotron-70B-Instruct-HF"
|
| 10 |
+
pipe = pipeline("text-generation", model=model_name)
|
| 11 |
+
|
| 12 |
+
# Load the dataset from the cloned local directory
|
| 13 |
+
ds = load_dataset("./canadian-legal-data", split="train")
|
| 14 |
+
|
| 15 |
+
# Gradio Interface setup
|
| 16 |
+
def respond(
|
| 17 |
+
message,
|
| 18 |
+
history: list[tuple[str, str]],
|
| 19 |
+
system_message,
|
| 20 |
+
max_tokens,
|
| 21 |
+
temperature,
|
| 22 |
+
top_p,
|
| 23 |
+
):
|
| 24 |
+
messages = [{"role": "system", "content": system_message}]
|
| 25 |
+
|
| 26 |
+
for val in history:
|
| 27 |
+
if val[0]:
|
| 28 |
+
messages.append({"role": "user", "content": val[0]})
|
| 29 |
+
if val[1]:
|
| 30 |
+
messages.append({"role": "assistant", "content": val[1]})
|
| 31 |
+
|
| 32 |
+
messages.append({"role": "user", "content": message})
|
| 33 |
+
|
| 34 |
+
response = ""
|
| 35 |
+
|
| 36 |
+
for message in pipe(
|
| 37 |
+
prompt=message,
|
| 38 |
+
max_length=max_tokens,
|
| 39 |
+
do_sample=True,
|
| 40 |
+
temperature=temperature,
|
| 41 |
+
top_p=top_p,
|
| 42 |
+
):
|
| 43 |
+
token = message["generated_text"]
|
| 44 |
+
response += token
|
| 45 |
+
yield response
|
| 46 |
+
|
| 47 |
+
# Streamlit Interface setup
|
| 48 |
+
def streamlit_interface():
|
| 49 |
+
st.title("Canadian Legal Text Generator")
|
| 50 |
+
st.write("Enter a prompt related to Canadian legal data and generate text using Llama-3.1.")
|
| 51 |
+
|
| 52 |
+
# Show dataset sample
|
| 53 |
+
st.subheader("Sample Data from Canadian Legal Dataset:")
|
| 54 |
+
st.write(ds[:5]) # Display the first 5 rows of the dataset
|
| 55 |
+
|
| 56 |
+
# Prompt input
|
| 57 |
+
prompt = st.text_area("Enter your prompt:", placeholder="Type something...")
|
| 58 |
+
|
| 59 |
+
if st.button("Generate Response"):
|
| 60 |
+
if prompt:
|
| 61 |
+
# Generate text based on the prompt
|
| 62 |
+
with st.spinner("Generating response..."):
|
| 63 |
+
generated_text = pipe(prompt, max_length=100, do_sample=True, temperature=0.7)[0]["generated_text"]
|
| 64 |
+
st.write("**Generated Text:**")
|
| 65 |
+
st.write(generated_text)
|
| 66 |
+
else:
|
| 67 |
+
st.write("Please enter a prompt to generate a response.")
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
# Running Gradio and Streamlit interfaces
|
| 71 |
+
if __name__ == "__main__":
|
| 72 |
+
st.sidebar.title("Choose an Interface")
|
| 73 |
+
interface = st.sidebar.radio("Select", ("Streamlit", "Gradio"))
|
| 74 |
+
|
| 75 |
+
if interface == "Streamlit":
|
| 76 |
+
streamlit_interface()
|
| 77 |
else:
|
| 78 |
+
demo = gr.ChatInterface(
|
| 79 |
+
respond,
|
| 80 |
+
additional_inputs=[
|
| 81 |
+
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 82 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 83 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 84 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
|
| 85 |
+
],
|
| 86 |
+
)
|
| 87 |
+
demo.launch()
|