Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from openai import OpenAI
|
| 3 |
import os
|
| 4 |
-
import json
|
| 5 |
|
| 6 |
css = '''
|
| 7 |
.gradio-container{max-width: 1000px !important}
|
|
@@ -11,31 +12,13 @@ footer {
|
|
| 11 |
}
|
| 12 |
'''
|
| 13 |
|
| 14 |
-
# Access token for Hugging Face
|
| 15 |
ACCESS_TOKEN = os.getenv("HF_TOKEN")
|
| 16 |
|
| 17 |
-
# Initialize the client for the OpenAI model
|
| 18 |
client = OpenAI(
|
| 19 |
base_url="https://api-inference.huggingface.co/v1/",
|
| 20 |
api_key=ACCESS_TOKEN,
|
| 21 |
)
|
| 22 |
|
| 23 |
-
# File path for storing user preferences
|
| 24 |
-
USER_DATA_PATH = "user_data.json"
|
| 25 |
-
|
| 26 |
-
# Load user preferences if they exist
|
| 27 |
-
def load_user_preferences():
|
| 28 |
-
if os.path.exists(USER_DATA_PATH):
|
| 29 |
-
with open(USER_DATA_PATH, "r") as file:
|
| 30 |
-
return json.load(file)
|
| 31 |
-
return {}
|
| 32 |
-
|
| 33 |
-
# Save user preferences
|
| 34 |
-
def save_user_preferences(data):
|
| 35 |
-
with open(USER_DATA_PATH, "w") as file:
|
| 36 |
-
json.dump(data, file)
|
| 37 |
-
|
| 38 |
-
# Respond function that generates the assistant's reply
|
| 39 |
def respond(
|
| 40 |
message,
|
| 41 |
history: list[tuple[str, str]],
|
|
@@ -44,36 +27,6 @@ def respond(
|
|
| 44 |
temperature,
|
| 45 |
top_p,
|
| 46 |
):
|
| 47 |
-
# Load user preferences
|
| 48 |
-
user_data = load_user_preferences()
|
| 49 |
-
|
| 50 |
-
# Custom welcome message or save user input
|
| 51 |
-
if message.lower().startswith("my name is"):
|
| 52 |
-
user_data["name"] = message.split("is")[-1].strip()
|
| 53 |
-
save_user_preferences(user_data)
|
| 54 |
-
response = f"Nice to meet you, {user_data['name']}! How can I assist you with your travel plans today?"
|
| 55 |
-
yield response
|
| 56 |
-
return
|
| 57 |
-
|
| 58 |
-
if message.lower().startswith("i like to travel to"):
|
| 59 |
-
user_data["favorite_destination"] = message.split("to")[-1].strip()
|
| 60 |
-
save_user_preferences(user_data)
|
| 61 |
-
response = f"Got it! I noted that you enjoy traveling to {user_data['favorite_destination']}."
|
| 62 |
-
yield response
|
| 63 |
-
return
|
| 64 |
-
|
| 65 |
-
if message.lower().startswith("my budget is"):
|
| 66 |
-
user_data["budget"] = message.split("is")[-1].strip()
|
| 67 |
-
save_user_preferences(user_data)
|
| 68 |
-
response = f"Understood! I'll keep your budget of {user_data['budget']} in mind when suggesting travel options."
|
| 69 |
-
yield response
|
| 70 |
-
return
|
| 71 |
-
|
| 72 |
-
# Use user's name and preferences in responses if available
|
| 73 |
-
name = user_data.get("name", "Traveler")
|
| 74 |
-
favorite_destination = user_data.get("favorite_destination", "various places")
|
| 75 |
-
budget = user_data.get("budget", "not specified")
|
| 76 |
-
|
| 77 |
messages = [{"role": "system", "content": system_message}]
|
| 78 |
|
| 79 |
for val in history:
|
|
@@ -82,13 +35,11 @@ def respond(
|
|
| 82 |
if val[1]:
|
| 83 |
messages.append({"role": "assistant", "content": val[1]})
|
| 84 |
|
| 85 |
-
# Add the current user message
|
| 86 |
messages.append({"role": "user", "content": message})
|
| 87 |
|
| 88 |
-
response =
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
for message in client.chat.completions.create(
|
| 92 |
model="meta-llama/Meta-Llama-3.1-8B-Instruct",
|
| 93 |
max_tokens=max_tokens,
|
| 94 |
stream=True,
|
|
@@ -97,17 +48,14 @@ def respond(
|
|
| 97 |
messages=messages,
|
| 98 |
):
|
| 99 |
token = message.choices[0].delta.content
|
|
|
|
| 100 |
response += token
|
| 101 |
yield response
|
| 102 |
|
| 103 |
-
# Gradio interface
|
| 104 |
demo = gr.ChatInterface(
|
| 105 |
respond,
|
| 106 |
additional_inputs=[
|
| 107 |
-
gr.Textbox(
|
| 108 |
-
value="You are a friendly travel assistant. Offer personalized travel tips and remember user preferences.",
|
| 109 |
-
label="System message"
|
| 110 |
-
),
|
| 111 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 112 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 113 |
gr.Slider(
|
|
@@ -117,10 +65,10 @@ demo = gr.ChatInterface(
|
|
| 117 |
step=0.05,
|
| 118 |
label="Top-P",
|
| 119 |
),
|
|
|
|
| 120 |
],
|
| 121 |
css=css,
|
| 122 |
theme="allenai/gradio-theme",
|
| 123 |
)
|
| 124 |
-
|
| 125 |
if __name__ == "__main__":
|
| 126 |
demo.launch()
|
|
|
|
| 1 |
+
#refer llama recipes for more info https://github.com/huggingface/huggingface-llama-recipes/blob/main/inference-api.ipynb
|
| 2 |
+
#huggingface-llama-recipes : https://github.com/huggingface/huggingface-llama-recipes/tree/main
|
| 3 |
import gradio as gr
|
| 4 |
from openai import OpenAI
|
| 5 |
import os
|
|
|
|
| 6 |
|
| 7 |
css = '''
|
| 8 |
.gradio-container{max-width: 1000px !important}
|
|
|
|
| 12 |
}
|
| 13 |
'''
|
| 14 |
|
|
|
|
| 15 |
ACCESS_TOKEN = os.getenv("HF_TOKEN")
|
| 16 |
|
|
|
|
| 17 |
client = OpenAI(
|
| 18 |
base_url="https://api-inference.huggingface.co/v1/",
|
| 19 |
api_key=ACCESS_TOKEN,
|
| 20 |
)
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
def respond(
|
| 23 |
message,
|
| 24 |
history: list[tuple[str, str]],
|
|
|
|
| 27 |
temperature,
|
| 28 |
top_p,
|
| 29 |
):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
messages = [{"role": "system", "content": system_message}]
|
| 31 |
|
| 32 |
for val in history:
|
|
|
|
| 35 |
if val[1]:
|
| 36 |
messages.append({"role": "assistant", "content": val[1]})
|
| 37 |
|
|
|
|
| 38 |
messages.append({"role": "user", "content": message})
|
| 39 |
|
| 40 |
+
response = ""
|
| 41 |
+
|
| 42 |
+
for message in client.chat.completions.create(
|
|
|
|
| 43 |
model="meta-llama/Meta-Llama-3.1-8B-Instruct",
|
| 44 |
max_tokens=max_tokens,
|
| 45 |
stream=True,
|
|
|
|
| 48 |
messages=messages,
|
| 49 |
):
|
| 50 |
token = message.choices[0].delta.content
|
| 51 |
+
|
| 52 |
response += token
|
| 53 |
yield response
|
| 54 |
|
|
|
|
| 55 |
demo = gr.ChatInterface(
|
| 56 |
respond,
|
| 57 |
additional_inputs=[
|
| 58 |
+
gr.Textbox(value="", label="System message"),
|
|
|
|
|
|
|
|
|
|
| 59 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 60 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 61 |
gr.Slider(
|
|
|
|
| 65 |
step=0.05,
|
| 66 |
label="Top-P",
|
| 67 |
),
|
| 68 |
+
|
| 69 |
],
|
| 70 |
css=css,
|
| 71 |
theme="allenai/gradio-theme",
|
| 72 |
)
|
|
|
|
| 73 |
if __name__ == "__main__":
|
| 74 |
demo.launch()
|