Rob Learsch commited on
Commit
a256478
·
1 Parent(s): 512a788

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -40
app.py CHANGED
@@ -22,9 +22,9 @@ def return_image(artist):
22
  if artist == "Radiohead":
23
  return "radiohead.png"
24
  if artist == "Kendrick Lamar":
25
- return base64.b64encode(open("kendrick.png", "rb").read()).decode()
26
  if artist == "Google Gemma":
27
- return base64.b64encode(open("gemma.png", "rb").read()).decode()
28
 
29
  def find_most_relevant_lyric(lyrics, user_input):
30
  user_doc = nlp(user_input)
@@ -49,46 +49,51 @@ client = InferenceClient(model="google/gemma-2-2b-it", token=HF_API_KEY)
49
 
50
  system_message = "Please limit your response to only a few sentences."
51
  # Function to generate chatbot responses
52
- def chat_with_musician(user_input, history, artist):
53
- if history is None:
54
- history = []
55
- messages = []
56
- #messages.append({"role": "user", "content": system_message})
57
-
58
- for dict in history[-5*2:]: # Keep only the last 5 exchanges, #I think this must be even to ensure that the last user message is included
59
- messages.append(dict)
60
- # Add the latest user message
61
- messages.append({"role": "user", "content": system_message + "\n\n" + user_input})
62
- try:
63
- response = client.chat_completion(
64
- messages=messages,
65
- model="google/gemma-2-2b-it",
66
- max_tokens=256,
67
- temperature=0.7,
68
- top_p=0.9
69
- )
70
- gemma_response= response["choices"][0]["message"]["content"]
71
- except Exception as e:
72
- return f"Error: {str(e)}"
73
-
74
- history.append({"role": "user", "content": user_input})
75
- history.append({"role": "assistant", "content": gemma_response})
76
- if artist == "Radiohead":
77
- lyric_response = find_most_relevant_lyric(stitched_lyrics,
78
- gemma_response)
79
- if artist == "Google Gemma":
80
- lyric_response = gemma_response
81
- return lyric_response
82
-
83
- with gr.Blocks() as demo:
84
- gr.Markdown("Start typing below and then click **Run** to see the output.")
85
- with gr.Row():
86
- inp =gr.Dropdown(choices=["Radiohead", "Kendrick Lamar","Google Gemma"],
87
  value="Radiohead",
88
  label="Select artist",
89
- info="More coming soon"),
90
- out = gr.Image(return_image, inputs=inp, label="Thumbnail",height=size, width=size)
91
- # Connect the input to the output using the defined functions.
 
 
 
 
 
 
 
 
 
92
  #inp.change(fn=return_image, inputs=inp, outputs=out)
93
  #gr.ChatInterface(
94
  # chat_with_musician,
 
22
  if artist == "Radiohead":
23
  return "radiohead.png"
24
  if artist == "Kendrick Lamar":
25
+ return "kendrick.png"
26
  if artist == "Google Gemma":
27
+ return "gemma.png"
28
 
29
  def find_most_relevant_lyric(lyrics, user_input):
30
  user_doc = nlp(user_input)
 
49
 
50
  system_message = "Please limit your response to only a few sentences."
51
  # Function to generate chatbot responses
52
+ #def chat_with_musician(user_input, history, artist):
53
+ # if history is None:
54
+ # history = []
55
+ # messages = []
56
+ # #messages.append({"role": "user", "content": system_message})
57
+ #
58
+ # for dict in history[-5*2:]: # Keep only the last 5 exchanges, #I think this must be even to ensure that the last user message is included
59
+ # messages.append(dict)
60
+ # # Add the latest user message
61
+ # messages.append({"role": "user", "content": system_message + "\n\n" + user_input})
62
+ # try:
63
+ # response = client.chat_completion(
64
+ # messages=messages,
65
+ # model="google/gemma-2-2b-it",
66
+ # max_tokens=256,
67
+ # temperature=0.7,
68
+ # top_p=0.9
69
+ # )
70
+ # gemma_response= response["choices"][0]["message"]["content"]
71
+ # except Exception as e:
72
+ # return f"Error: {str(e)}"
73
+ #
74
+ # history.append({"role": "user", "content": user_input})
75
+ # history.append({"role": "assistant", "content": gemma_response})
76
+ # if artist == "Radiohead":
77
+ # lyric_response = find_most_relevant_lyric(stitched_lyrics,
78
+ # gemma_response)
79
+ # if artist == "Google Gemma":
80
+ # lyric_response = gemma_response
81
+ # return lyric_response
82
+ Artist_dropdown = gr.Dropdown(choices=["Radiohead", "Kendrick Lamar","Google Gemma"],
 
 
 
 
83
  value="Radiohead",
84
  label="Select artist",
85
+ info="More coming soon")
86
+ with gr.Blocks() as demo:
87
+ gr.Markdown("Select someone to chat with.")
88
+ with gr.Row():
89
+ gr.Interface(
90
+ fn = return_image,
91
+ inputs = Artist_dropdown,
92
+ outputs = gr.Image(label="Thumbnail",height=size, width=size),
93
+ live=True,
94
+ )
95
+
96
+ # Connect the input to the output using the defined functions.
97
  #inp.change(fn=return_image, inputs=inp, outputs=out)
98
  #gr.ChatInterface(
99
  # chat_with_musician,