Kim Juwon
commited on
Commit
·
e687f2e
1
Parent(s):
64e473e
update UI/UX
Browse files
app.py
CHANGED
|
@@ -1,10 +1,12 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
"""
|
| 5 |
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 6 |
"""
|
| 7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 8 |
|
| 9 |
def create_system_prompt(agent_type, personality, expertise_level, language):
|
| 10 |
base_prompt = f"""You are a {agent_type} movie recommendation agent with the following characteristics:
|
|
@@ -37,30 +39,44 @@ def respond(
|
|
| 37 |
):
|
| 38 |
# Create system prompt
|
| 39 |
system_message = create_system_prompt(agent_type, personality, expertise_level, language)
|
|
|
|
|
|
|
| 40 |
messages = [{"role": "system", "content": system_message}]
|
| 41 |
-
|
| 42 |
-
# Add genre and mood information to user input
|
| 43 |
-
enhanced_message = f"Genre: {genre}\nMood: {mood}\nUser request: {message}"
|
| 44 |
|
|
|
|
| 45 |
for val in history:
|
| 46 |
if val[0]:
|
| 47 |
messages.append({"role": "user", "content": val[0]})
|
| 48 |
if val[1]:
|
| 49 |
messages.append({"role": "assistant", "content": val[1]})
|
| 50 |
-
|
|
|
|
|
|
|
| 51 |
messages.append({"role": "user", "content": enhanced_message})
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
messages,
|
| 56 |
-
max_tokens
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
def reset_chat():
|
| 66 |
return None
|
|
@@ -103,7 +119,7 @@ with gr.Blocks() as demo:
|
|
| 103 |
container=False
|
| 104 |
)
|
| 105 |
with gr.Row():
|
| 106 |
-
submit = gr.Button("
|
| 107 |
clear = gr.Button("Clear Chat", size="sm")
|
| 108 |
|
| 109 |
with gr.Column(scale=1):
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import json
|
| 4 |
+
|
| 5 |
+
MODAL_ENDPOINT = "https://kim-ju-won--llama3-70b-chat.modal.run/llama3_chat_endpoint"
|
| 6 |
|
| 7 |
"""
|
| 8 |
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 9 |
"""
|
|
|
|
| 10 |
|
| 11 |
def create_system_prompt(agent_type, personality, expertise_level, language):
|
| 12 |
base_prompt = f"""You are a {agent_type} movie recommendation agent with the following characteristics:
|
|
|
|
| 39 |
):
|
| 40 |
# Create system prompt
|
| 41 |
system_message = create_system_prompt(agent_type, personality, expertise_level, language)
|
| 42 |
+
|
| 43 |
+
# Prepare messages for the API
|
| 44 |
messages = [{"role": "system", "content": system_message}]
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
+
# Add conversation history
|
| 47 |
for val in history:
|
| 48 |
if val[0]:
|
| 49 |
messages.append({"role": "user", "content": val[0]})
|
| 50 |
if val[1]:
|
| 51 |
messages.append({"role": "assistant", "content": val[1]})
|
| 52 |
+
|
| 53 |
+
# Add current message with genre and mood
|
| 54 |
+
enhanced_message = f"Genre: {genre}\nMood: {mood}\nUser request: {message}"
|
| 55 |
messages.append({"role": "user", "content": enhanced_message})
|
| 56 |
+
|
| 57 |
+
# Prepare request payload
|
| 58 |
+
payload = {
|
| 59 |
+
"messages": messages,
|
| 60 |
+
"max_tokens": max_tokens,
|
| 61 |
+
"temperature": temperature,
|
| 62 |
+
"top_p": top_p
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
try:
|
| 66 |
+
# Send request to Modal endpoint
|
| 67 |
+
response = requests.post(
|
| 68 |
+
MODAL_ENDPOINT,
|
| 69 |
+
json=payload,
|
| 70 |
+
headers={"Content-Type": "application/json"}
|
| 71 |
+
)
|
| 72 |
+
response.raise_for_status()
|
| 73 |
+
|
| 74 |
+
# Get response from Modal
|
| 75 |
+
result = response.json()
|
| 76 |
+
return result.get("response", "Sorry, I couldn't process your request.")
|
| 77 |
+
|
| 78 |
+
except Exception as e:
|
| 79 |
+
return f"Error: {str(e)}"
|
| 80 |
|
| 81 |
def reset_chat():
|
| 82 |
return None
|
|
|
|
| 119 |
container=False
|
| 120 |
)
|
| 121 |
with gr.Row():
|
| 122 |
+
submit = gr.Button("Send Chat", variant="primary", size="sm")
|
| 123 |
clear = gr.Button("Clear Chat", size="sm")
|
| 124 |
|
| 125 |
with gr.Column(scale=1):
|