Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from huggingface_hub import InferenceClient
|
| 2 |
+
import gradio as gr
|
| 3 |
+
|
| 4 |
+
client = InferenceClient("meta-llama/Meta-Llama-3.1-8B")
|
| 5 |
+
|
| 6 |
+
def format_prompt(message, history):
|
| 7 |
+
fixed_prompt= """ """
|
| 8 |
+
prompt = f"<s>{fixed_prompt}"
|
| 9 |
+
for user_prompt, bot_response in history:
|
| 10 |
+
prompt += f"\n User:{user_prompt}\n LLM Response:{bot_response}"
|
| 11 |
+
prompt += f"\nUser: {message}\nLLM Response:"
|
| 12 |
+
|
| 13 |
+
return prompt
|
| 14 |
+
|
| 15 |
+
def generate(
|
| 16 |
+
prompt, history, temperature=0.1, max_new_tokens=2048, top_p=0.8, repetition_penalty=1.0,
|
| 17 |
+
):
|
| 18 |
+
temperature = float(temperature)
|
| 19 |
+
if temperature < 1e-2:
|
| 20 |
+
temperature = 1e-2
|
| 21 |
+
top_p = float(top_p)
|
| 22 |
+
|
| 23 |
+
generate_kwargs = dict(
|
| 24 |
+
temperature=temperature,
|
| 25 |
+
max_new_tokens=max_new_tokens,
|
| 26 |
+
top_p=top_p,
|
| 27 |
+
repetition_penalty=repetition_penalty,
|
| 28 |
+
do_sample=True,
|
| 29 |
+
seed=42,
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
formatted_prompt = format_prompt(prompt, history)
|
| 33 |
+
|
| 34 |
+
stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
|
| 35 |
+
|
| 36 |
+
yield stream
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
demo = gr.ChatInterface (fn=generate,
|
| 41 |
+
title="Mood-Based Music Recommender",
|
| 42 |
+
retry_btn=None,
|
| 43 |
+
undo_btn=None,
|
| 44 |
+
clear_btn=None,
|
| 45 |
+
description="<span style='font-size: larger; font-weight: bold;'>Hi! I'm your music buddy—tell me about your mood and the type of tunes you're in the mood for today!</span>",
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
demo.queue().launch()
|