File size: 3,246 Bytes
f4291f8
a75c2ce
7d1fd90
f2878f5
256a97d
f4291f8
7330c41
15f4e8e
 
 
 
5af982a
 
 
b152500
6e14cea
c17ad3c
7d1fd90
5af982a
 
7082356
78b603b
7d1fd90
 
 
61300f7
256a97d
578cce2
1ea2d65
578cce2
7d1fd90
 
1b98803
2b6d89b
d6cfa8c
 
 
 
bbd86c5
d6cfa8c
 
 
 
 
 
4d55877
acbf5b8
af4b0d5
 
cc99dc7
d60a7dd
 
 
 
 
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
import streamlit as st
import os
import requests
import json
import re

with open('./styles.css') as f:
    css = f.read()

st.markdown(f'<style>{css}</style>', unsafe_allow_html=True)

#from huggingface_hub.inference_api import InferenceApi
#inference = InferenceApi(repo_id="BenBranyon/tinyllama-sumbot-peft", token=os.environ['HF_TOKEN'])
headers = {"Content-Type": "application/json","Authorization": f"Bearer {os.environ['HF_TOKEN']}"} #TOKEN HUGGING FACE
API_URL = "https://api-inference.huggingface.co/models/BenBranyon/zephyr-sumbot-all-songs"

def query(payload):
    json_body = {
        "inputs": f"<|system|> As an AI, you channel the poetic lyrical skill of the artist Sumkilla. Your rap lyrics focus on technology, and social activism, embodying the spirit of Sumkilla, a multi-disciplinary, award-winning artist with a foundation in writing rap and hip-hop. Your purpose is to challenge and expand the boundaries of art and expression, critically examining societal norms through a lens that actively de-centers whiteness, maleness, and Western thinking. Your work is fueled by a passion for liberation, aiming to dismantle oppressive systems and advocate for the freedom of all occupied lands, along with the abolition of police forces. With a sophisticated understanding of AI's role in advancing the harmony between humanity and nature, you aim to produce rap lyrics that promote awareness and human evolution, utilizing humor and a distinctive voice to connect deeply and honor humanity. Each of your responses should be rap lyrics with each bar on a new line and rhyming as often as posssible.</s><|user|>{payload}</s><|assistant|>",
        "parameters": {"min_new_tokens":222, "max_new_tokens":222, "top_p":0.95, "temperature":1.0, "repetition_penalty":1.2},
        "options": {"wait_for_model":True}
    }
    data = json.dumps(json_body)
    response = requests.request("POST", API_URL, headers=headers, data=data)
    try:
        data = json.loads(response.content.decode("utf-8"))
        generated_text = data[0]['generated_text']
        cleaned_text = re.sub(r'<|system|>.*?<|assistant|>', '', generated_text, flags=re.DOTALL)
        cleaned_text = re.sub(r'\|\|\/s\|user\|\/s\|\|', '', cleaned_text) 
        return cleaned_text
    except:
        return response

messages = st.container()

# Initialize chat history
if "messages" not in st.session_state:
    st.session_state.messages = []
    st.session_state.messages.append({"role": "assistant", "content": "Hello human what would you like me to rap about today?"})

# Display chat messages from history on app rerun
for message in st.session_state.messages:
    with st.chat_message(message["role"]):
        st.markdown(message["content"])
        
if prompt := st.chat_input("Subject of the song"):
    st.session_state.messages.append({"role": "user", "content": prompt})
    with st.chat_message("user"):
        st.markdown(prompt)
    prompt = f"Write a rap in the style of the artist Sumkilla about {prompt}"
    with st.spinner(text=f"Generating lyrics..."):
        output = query(prompt)
        st.session_state.messages.append({"role": "assistant", "content": output})
        with st.chat_message("assistant"):
            st.markdown(output)