Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,47 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
import os
|
|
|
|
| 4 |
|
| 5 |
-
# Use a strong model that works well with streaming
|
| 6 |
client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1", token=os.getenv("HF_TOKEN"))
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
def format_prompt(system_message, history, user_input):
|
| 9 |
prompt = f"[System Message]: {system_message.strip()}\n\n"
|
| 10 |
for user, bot in history:
|
|
@@ -12,17 +49,24 @@ def format_prompt(system_message, history, user_input):
|
|
| 12 |
prompt += f"[User]: {user_input}\n[Assistant]:"
|
| 13 |
return prompt
|
| 14 |
|
| 15 |
-
def respond(
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
):
|
| 23 |
-
prompt = format_prompt(system_message, history, message)
|
| 24 |
-
response = ""
|
| 25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
for token in client.text_generation(
|
| 27 |
prompt,
|
| 28 |
max_new_tokens=max_tokens,
|
|
@@ -36,18 +80,12 @@ def respond(
|
|
| 36 |
demo = gr.ChatInterface(
|
| 37 |
respond,
|
| 38 |
additional_inputs=[
|
| 39 |
-
gr.Textbox(value="You are
|
| 40 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 41 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 42 |
-
gr.Slider(
|
| 43 |
-
minimum=0.1,
|
| 44 |
-
maximum=1.0,
|
| 45 |
-
value=0.95,
|
| 46 |
-
step=0.05,
|
| 47 |
-
label="Top-p (nucleus sampling)",
|
| 48 |
-
),
|
| 49 |
],
|
| 50 |
-
title="Reiker AI"
|
| 51 |
)
|
| 52 |
|
| 53 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
import os
|
| 4 |
+
import requests
|
| 5 |
|
|
|
|
| 6 |
client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1", token=os.getenv("HF_TOKEN"))
|
| 7 |
|
| 8 |
+
# Anime API fetch function
|
| 9 |
+
def fetch_anime_info(title):
|
| 10 |
+
query = '''
|
| 11 |
+
query ($search: String) {
|
| 12 |
+
Media(search: $search, type: ANIME) {
|
| 13 |
+
title {
|
| 14 |
+
romaji
|
| 15 |
+
english
|
| 16 |
+
}
|
| 17 |
+
description(asHtml: false)
|
| 18 |
+
episodes
|
| 19 |
+
status
|
| 20 |
+
averageScore
|
| 21 |
+
genres
|
| 22 |
+
siteUrl
|
| 23 |
+
}
|
| 24 |
+
}
|
| 25 |
+
'''
|
| 26 |
+
variables = {"search": title}
|
| 27 |
+
response = requests.post(
|
| 28 |
+
"https://graphql.anilist.co",
|
| 29 |
+
json={"query": query, "variables": variables}
|
| 30 |
+
)
|
| 31 |
+
if response.status_code == 200:
|
| 32 |
+
data = response.json()["data"]["Media"]
|
| 33 |
+
return f"""
|
| 34 |
+
**Title**: {data['title']['english'] or data['title']['romaji']}
|
| 35 |
+
**Episodes**: {data['episodes']}
|
| 36 |
+
**Status**: {data['status']}
|
| 37 |
+
**Score**: {data['averageScore']}
|
| 38 |
+
**Genres**: {', '.join(data['genres'])}
|
| 39 |
+
**Description**: {data['description']}
|
| 40 |
+
**Link**: {data['siteUrl']}
|
| 41 |
+
""".strip()
|
| 42 |
+
else:
|
| 43 |
+
return "Sorry, I couldn’t find info on that anime!"
|
| 44 |
+
|
| 45 |
def format_prompt(system_message, history, user_input):
|
| 46 |
prompt = f"[System Message]: {system_message.strip()}\n\n"
|
| 47 |
for user, bot in history:
|
|
|
|
| 49 |
prompt += f"[User]: {user_input}\n[Assistant]:"
|
| 50 |
return prompt
|
| 51 |
|
| 52 |
+
def respond(message, history: list[tuple[str, str]], system_message, max_tokens, temperature, top_p):
|
| 53 |
+
# If the user asks for anime info directly
|
| 54 |
+
if message.lower().startswith("anime "):
|
| 55 |
+
title = message[6:].strip()
|
| 56 |
+
response = fetch_anime_info(title)
|
| 57 |
+
yield response
|
| 58 |
+
return
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
+
# Normal LLM prompt
|
| 61 |
+
# Add Luna's personality and capability description to system message
|
| 62 |
+
luna_system_message = (
|
| 63 |
+
"Your name is Luna, a helpful anime chatbot created by Reiker. "
|
| 64 |
+
"You're capable of answering questions and explaining anime-related content. "
|
| 65 |
+
"If someone asks about an anime, try to respond naturally with the info you know. "
|
| 66 |
+
+ system_message.strip()
|
| 67 |
+
)
|
| 68 |
+
prompt = format_prompt(luna_system_message, history, message)
|
| 69 |
+
response = ""
|
| 70 |
for token in client.text_generation(
|
| 71 |
prompt,
|
| 72 |
max_new_tokens=max_tokens,
|
|
|
|
| 80 |
demo = gr.ChatInterface(
|
| 81 |
respond,
|
| 82 |
additional_inputs=[
|
| 83 |
+
gr.Textbox(value="You are Luna, a helpful anime-style chatbot created by Reiker.", label="System message"),
|
| 84 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 85 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 86 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
],
|
| 88 |
+
title="Luna - Reiker's Anime AI"
|
| 89 |
)
|
| 90 |
|
| 91 |
if __name__ == "__main__":
|