James Edmunds commited on
Commit
ac06d04
·
1 Parent(s): 94d0ba0

fix: Restore full app functionality

Browse files
Files changed (1) hide show
  1. app.py +109 -1
app.py CHANGED
@@ -1,4 +1,112 @@
1
  """Main Streamlit application for lyric generation."""
 
2
  import streamlit as st
 
 
3
 
4
- st.write("Hello World!")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  """Main Streamlit application for lyric generation."""
2
+ import os
3
  import streamlit as st
4
+ from src.generator.generator import LyricGenerator
5
+ from config.settings import Settings
6
 
7
+ # Set SQLite path for local development
8
+ if not Settings.is_huggingface():
9
+ os.environ['DYLD_LIBRARY_PATH'] = '/usr/local/opt/sqlite/lib'
10
+
11
+ def initialize_generator():
12
+ """Initialize the generator with proper error handling"""
13
+ try:
14
+ # Initialize generator
15
+ st.info("Loading embeddings...")
16
+ generator = LyricGenerator()
17
+ st.success("Embeddings loaded successfully!")
18
+ return generator
19
+
20
+ except Exception as e:
21
+ st.error(f"Failed to initialize generator: {str(e)}")
22
+ return None
23
+
24
+ def main():
25
+ """Main application function"""
26
+ st.set_page_config(
27
+ page_title="SongLift LyrGen",
28
+ page_icon="🎵",
29
+ layout="wide"
30
+ )
31
+
32
+ st.title("SongLift LyrGen")
33
+
34
+ # Initialize generator on first run
35
+ if 'generator' not in st.session_state:
36
+ generator = initialize_generator()
37
+ if generator is None:
38
+ st.stop()
39
+ st.session_state.generator = generator
40
+ st.session_state.chat_history = []
41
+ st.session_state.current_lyrics = None
42
+
43
+ # Display chat history
44
+ for message in st.session_state.chat_history:
45
+ user_msg, assistant_msg = message
46
+ with st.chat_message("user"):
47
+ st.write(user_msg)
48
+ with st.chat_message("assistant"):
49
+ st.markdown(f"```\n{assistant_msg}\n```")
50
+
51
+ # Chat interface
52
+ prompt = "Enter your prompt (ask for new lyrics or modify existing ones)..."
53
+ user_input = st.chat_input(prompt)
54
+
55
+ if user_input:
56
+ with st.chat_message("user"):
57
+ st.write(user_input)
58
+
59
+ with st.chat_message("assistant"):
60
+ try:
61
+ with st.status("Generating lyrics..."):
62
+ response = st.session_state.generator.generate_lyrics(
63
+ user_input,
64
+ st.session_state.chat_history
65
+ )
66
+
67
+ # Store the response
68
+ lyrics = response['answer']
69
+ st.markdown(f"```\n{lyrics}\n```")
70
+ st.session_state.current_lyrics = lyrics
71
+
72
+ # Display sources with content
73
+ with st.expander("View Sources and Context"):
74
+ st.write("### Top Retrieved Contexts")
75
+ for detail in response["context_details"]:
76
+ st.write(
77
+ f"\n**{detail['artist']} - {detail['song']}** "
78
+ f"(Similarity: {detail['similarity']}%)"
79
+ )
80
+ st.write("Content snippet:")
81
+ st.text(detail['content'])
82
+ st.write("---")
83
+
84
+ st.write("\n### All Similar Sources")
85
+ seen_sources = set()
86
+ unique_sources = []
87
+
88
+ for doc, score in response["source_documents_with_scores"]:
89
+ source_key = (
90
+ doc.metadata['artist'],
91
+ doc.metadata['song_title']
92
+ )
93
+ if source_key not in seen_sources:
94
+ seen_sources.add(source_key)
95
+ unique_sources.append((doc, score))
96
+
97
+ for doc, score in unique_sources:
98
+ similarity = round((1 - score) * 100, 2)
99
+ st.write(
100
+ f"- {doc.metadata['artist']} - "
101
+ f"{doc.metadata['song_title']} "
102
+ f"(Similarity: {similarity}%)"
103
+ )
104
+
105
+ # Update chat history
106
+ st.session_state.chat_history.append((user_input, lyrics))
107
+
108
+ except Exception as e:
109
+ st.error(f"Error generating lyrics: {str(e)}")
110
+
111
+ if __name__ == "__main__":
112
+ main()