Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,43 +1,70 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
|
|
|
| 3 |
|
|
|
|
| 4 |
client = InferenceClient("Futuresony/future_ai_12_10_2024.gguf")
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
return prompt
|
| 16 |
|
| 17 |
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
response
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
| 23 |
temperature=temperature,
|
| 24 |
top_p=top_p,
|
| 25 |
-
)
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
#
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
|
|
|
| 32 |
demo = gr.ChatInterface(
|
| 33 |
respond,
|
| 34 |
additional_inputs=[
|
| 35 |
-
gr.Textbox(value="You are a friendly
|
| 36 |
-
gr.Slider(minimum=1, maximum=
|
| 37 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 38 |
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
|
| 39 |
],
|
| 40 |
)
|
| 41 |
-
|
| 42 |
if __name__ == "__main__":
|
| 43 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
+
from collections import defaultdict
|
| 4 |
|
| 5 |
+
# Initialize the model client
|
| 6 |
client = InferenceClient("Futuresony/future_ai_12_10_2024.gguf")
|
| 7 |
|
| 8 |
+
# Store user preferences & history
|
| 9 |
+
user_preferences = defaultdict(int) # Tracks keywords & topics
|
| 10 |
+
session_histories = defaultdict(list) # Stores conversation history per session
|
| 11 |
|
| 12 |
+
def extract_keywords(text):
|
| 13 |
+
"""Extracts simple keywords from user input."""
|
| 14 |
+
words = text.lower().split()
|
| 15 |
+
common_words = {"the", "is", "a", "and", "to", "of", "in", "it", "you", "for"} # Ignore common words
|
| 16 |
+
return [word for word in words if word not in common_words]
|
|
|
|
| 17 |
|
| 18 |
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
| 19 |
+
session_id = id(history) # Unique ID for each session
|
| 20 |
+
session_history = session_histories[session_id] # Retrieve session history
|
| 21 |
+
|
| 22 |
+
# Extract keywords & update preferences
|
| 23 |
+
keywords = extract_keywords(message)
|
| 24 |
+
for kw in keywords:
|
| 25 |
+
user_preferences[kw] += 1
|
| 26 |
+
|
| 27 |
+
# Add past conversation to message history
|
| 28 |
+
messages = [{"role": "system", "content": system_message}]
|
| 29 |
+
for user_msg, bot_response in session_history:
|
| 30 |
+
messages.append({"role": "user", "content": user_msg})
|
| 31 |
+
messages.append({"role": "assistant", "content": bot_response})
|
| 32 |
+
|
| 33 |
+
# Append current user message
|
| 34 |
+
messages.append({"role": "user", "content": message})
|
| 35 |
|
| 36 |
+
# Generate response from model
|
| 37 |
+
response = ""
|
| 38 |
+
for message in client.chat_completion(
|
| 39 |
+
messages,
|
| 40 |
+
max_tokens=max_tokens,
|
| 41 |
+
stream=True,
|
| 42 |
temperature=temperature,
|
| 43 |
top_p=top_p,
|
| 44 |
+
):
|
| 45 |
+
token = message.choices[0].delta.content
|
| 46 |
+
response += token
|
| 47 |
+
yield response # Stream response to user
|
| 48 |
|
| 49 |
+
# Save to session history
|
| 50 |
+
session_history.append((message, response))
|
| 51 |
+
|
| 52 |
+
# Optionally, adapt responses based on learned preferences
|
| 53 |
+
most_asked = max(user_preferences, key=user_preferences.get, default=None)
|
| 54 |
+
if most_asked and most_asked in message.lower():
|
| 55 |
+
response += f"\n\nI see you're interested in {most_asked} a lot! Want to explore more details?"
|
| 56 |
+
yield response # Update response with learning behavior
|
| 57 |
|
| 58 |
+
# Create Chat Interface
|
| 59 |
demo = gr.ChatInterface(
|
| 60 |
respond,
|
| 61 |
additional_inputs=[
|
| 62 |
+
gr.Textbox(value="You are a friendly chatbot that learns user interests.", label="System message"),
|
| 63 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 64 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 65 |
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
|
| 66 |
],
|
| 67 |
)
|
| 68 |
+
|
| 69 |
if __name__ == "__main__":
|
| 70 |
demo.launch()
|