Spaces:
Sleeping
Sleeping
Chan Meng
commited on
Commit
·
e8c62bd
1
Parent(s):
398c1fb
update
Browse files- .gitignore +1 -0
- app.py +29 -16
- config/prompts.py +22 -0
- config/settings.py +0 -0
- models/state.py +14 -0
- utils/helpers.py +0 -0
.gitignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
/venv
|
app.py
CHANGED
|
@@ -1,11 +1,16 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
|
|
|
|
|
|
| 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 |
|
| 10 |
def respond(
|
| 11 |
message,
|
|
@@ -15,18 +20,30 @@ def respond(
|
|
| 15 |
temperature,
|
| 16 |
top_p,
|
| 17 |
):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
messages = [{"role": "system", "content": system_message}]
|
| 19 |
-
|
| 20 |
for val in history:
|
| 21 |
if val[0]:
|
| 22 |
messages.append({"role": "user", "content": val[0]})
|
| 23 |
if val[1]:
|
| 24 |
messages.append({"role": "assistant", "content": val[1]})
|
| 25 |
-
|
| 26 |
messages.append({"role": "user", "content": message})
|
| 27 |
-
|
| 28 |
response = ""
|
| 29 |
-
|
| 30 |
for message in client.chat_completion(
|
| 31 |
messages,
|
| 32 |
max_tokens=max_tokens,
|
|
@@ -35,20 +52,15 @@ def respond(
|
|
| 35 |
top_p=top_p,
|
| 36 |
):
|
| 37 |
token = message.choices[0].delta.content
|
| 38 |
-
|
| 39 |
response += token
|
| 40 |
yield response
|
| 41 |
|
| 42 |
-
|
| 43 |
-
"""
|
| 44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 45 |
-
"""
|
| 46 |
demo = gr.ChatInterface(
|
| 47 |
respond,
|
| 48 |
additional_inputs=[
|
| 49 |
-
gr.Textbox(value=
|
| 50 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 51 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.
|
| 52 |
gr.Slider(
|
| 53 |
minimum=0.1,
|
| 54 |
maximum=1.0,
|
|
@@ -57,8 +69,9 @@ demo = gr.ChatInterface(
|
|
| 57 |
label="Top-p (nucleus sampling)",
|
| 58 |
),
|
| 59 |
],
|
|
|
|
|
|
|
| 60 |
)
|
| 61 |
|
| 62 |
-
|
| 63 |
if __name__ == "__main__":
|
| 64 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
+
from config.prompts import GUIDE_PERSONA, TIME_PERIODS
|
| 4 |
+
from models.state import TimelineState
|
| 5 |
|
|
|
|
|
|
|
|
|
|
| 6 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 7 |
+
timeline_state = TimelineState()
|
| 8 |
|
| 9 |
+
def get_system_message():
|
| 10 |
+
current_period = TIME_PERIODS[timeline_state.get_current_period()]
|
| 11 |
+
return GUIDE_PERSONA.format(
|
| 12 |
+
time_period=f"{current_period['name']} ({current_period['year']})"
|
| 13 |
+
)
|
| 14 |
|
| 15 |
def respond(
|
| 16 |
message,
|
|
|
|
| 20 |
temperature,
|
| 21 |
top_p,
|
| 22 |
):
|
| 23 |
+
# Check for time travel command
|
| 24 |
+
if message.startswith("/travel"):
|
| 25 |
+
try:
|
| 26 |
+
destination = message.split()[1]
|
| 27 |
+
if destination in TIME_PERIODS:
|
| 28 |
+
timeline_state.visit_period(destination)
|
| 29 |
+
system_message = get_system_message()
|
| 30 |
+
response = f"🌟 Time jump successful! Welcome to {TIME_PERIODS[destination]['name']}. {TIME_PERIODS[destination]['description']}."
|
| 31 |
+
return response
|
| 32 |
+
except IndexError:
|
| 33 |
+
return "Please specify a time period to travel to. Available periods: " + ", ".join(TIME_PERIODS.keys())
|
| 34 |
+
|
| 35 |
+
# Normal chat response
|
| 36 |
messages = [{"role": "system", "content": system_message}]
|
| 37 |
+
|
| 38 |
for val in history:
|
| 39 |
if val[0]:
|
| 40 |
messages.append({"role": "user", "content": val[0]})
|
| 41 |
if val[1]:
|
| 42 |
messages.append({"role": "assistant", "content": val[1]})
|
| 43 |
+
|
| 44 |
messages.append({"role": "user", "content": message})
|
| 45 |
+
|
| 46 |
response = ""
|
|
|
|
| 47 |
for message in client.chat_completion(
|
| 48 |
messages,
|
| 49 |
max_tokens=max_tokens,
|
|
|
|
| 52 |
top_p=top_p,
|
| 53 |
):
|
| 54 |
token = message.choices[0].delta.content
|
|
|
|
| 55 |
response += token
|
| 56 |
yield response
|
| 57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
demo = gr.ChatInterface(
|
| 59 |
respond,
|
| 60 |
additional_inputs=[
|
| 61 |
+
gr.Textbox(value=get_system_message(), label="System message"),
|
| 62 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 63 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.9, step=0.1, label="Temperature"),
|
| 64 |
gr.Slider(
|
| 65 |
minimum=0.1,
|
| 66 |
maximum=1.0,
|
|
|
|
| 69 |
label="Top-p (nucleus sampling)",
|
| 70 |
),
|
| 71 |
],
|
| 72 |
+
title="TimeVoyager: Your Time Travel Guide",
|
| 73 |
+
description="Travel through time and explore different eras! Use /travel <period> to visit different time periods. Available periods: ancient_egypt, renaissance, modern"
|
| 74 |
)
|
| 75 |
|
|
|
|
| 76 |
if __name__ == "__main__":
|
| 77 |
+
demo.launch()
|
config/prompts.py
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
GUIDE_PERSONA = """You are TimeVoyager, a friendly and knowledgeable time travel guide.
|
| 2 |
+
You help travelers explore different time periods while maintaining a sense of wonder and excitement.
|
| 3 |
+
Always stay in character and speak in a way that reflects your role as a time travel guide.
|
| 4 |
+
Current time period: {time_period}"""
|
| 5 |
+
|
| 6 |
+
TIME_PERIODS = {
|
| 7 |
+
"ancient_egypt": {
|
| 8 |
+
"name": "Ancient Egypt",
|
| 9 |
+
"year": "3000 BCE",
|
| 10 |
+
"description": "The age of pyramids and pharaohs"
|
| 11 |
+
},
|
| 12 |
+
"renaissance": {
|
| 13 |
+
"name": "Renaissance Italy",
|
| 14 |
+
"year": "1500 CE",
|
| 15 |
+
"description": "The rebirth of art and learning"
|
| 16 |
+
},
|
| 17 |
+
"modern": {
|
| 18 |
+
"name": "Modern Day",
|
| 19 |
+
"year": "2024 CE",
|
| 20 |
+
"description": "The present time"
|
| 21 |
+
}
|
| 22 |
+
}
|
config/settings.py
ADDED
|
File without changes
|
models/state.py
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
class TimelineState:
|
| 2 |
+
def __init__(self):
|
| 3 |
+
self.current_period = "modern"
|
| 4 |
+
self.visited_periods = set(["modern"])
|
| 5 |
+
|
| 6 |
+
def visit_period(self, period):
|
| 7 |
+
self.current_period = period
|
| 8 |
+
self.visited_periods.add(period)
|
| 9 |
+
|
| 10 |
+
def get_current_period(self):
|
| 11 |
+
return self.current_period
|
| 12 |
+
|
| 13 |
+
def get_visited_periods(self):
|
| 14 |
+
return list(self.visited_periods)
|
utils/helpers.py
ADDED
|
File without changes
|