Spaces:
Build error
Build error
Download app.py from APPenner/test-aven-2: direct link, hf CLI and curl.
- Browser
- Download file 4.39 kB
-
https://huggingface.co/spaces/APPenner/test-aven-2/resolve/main/app.py
- Command line
-
hf download hf://spaces/APPenner/test-aven-2/app.py
-
curl -L -o app.py https://huggingface.co/spaces/APPenner/test-aven-2/resolve/main/app.py
4.39 kB
| import gradio as gr | |
| from huggingface_hub import InferenceClient | |
| from transformers import AutoModelForCausalLM, AutoTokenizer | |
| import json | |
| import os | |
| # Initialize the Hugging Face inference client | |
| client = InferenceClient("HuggingFaceH4/zephyr-7b-beta") | |
| # Function to load memory from the memory.json file | |
| def load_memory(): | |
| memory_file = "memory.json" | |
| if os.path.exists(memory_file): | |
| try: | |
| with open(memory_file, "r") as f: | |
| data = f.read().strip() | |
| if data: | |
| return json.loads(data) # Return parsed memory | |
| else: | |
| return {} # Return an empty dictionary if file is empty | |
| except json.JSONDecodeError as e: | |
| print(f"Error loading memory: {e}") | |
| return {} # Return an empty dictionary if there's an error loading | |
| return {} # Return an empty dictionary if the file doesn't exist | |
| # Function to save the current memory to the memory.json file | |
| def save_memory(): | |
| try: | |
| with open("memory.json", "w") as f: | |
| json.dump(memory, f) | |
| print("Memory saved successfully.") | |
| except Exception as e: | |
| print(f"Error saving memory: {e}") | |
| # Initialize memory | |
| memory = load_memory() | |
| # Memory functions for remembering details | |
| def remember(key, value): | |
| memory[key] = value | |
| save_memory() # Save memory after adding new information | |
| def recall(name): | |
| return memory.get(name, None) | |
| # Respond function, processing messages and remembering names/facts | |
| def respond( | |
| message, | |
| history: list[tuple[str, str]], | |
| system_message, | |
| max_tokens, | |
| temperature, | |
| top_p, | |
| ): | |
| messages = [{"role": "system", "content": system_message}] | |
| for val in history: | |
| if val[0]: | |
| messages.append({"role": "user", "content": val[0]}) | |
| if val[1]: | |
| messages.append({"role": "assistant", "content": val[1]}) | |
| messages.append({"role": "user", "content": message}) | |
| # Check if the user is asking for their name | |
| if "what's my name" in message.lower(): | |
| name = recall("name") | |
| if name: | |
| return f"Your name is {name}." | |
| else: | |
| return "I don't know your name yet. You can tell me by saying 'I am [your name]'." | |
| # Check if the message contains "I am [name]" | |
| if message.lower().startswith("i am "): | |
| name = message[5:].strip() # Grab the name after "I am" | |
| remember("name", name) # Save the name in memory | |
| return f"Got it! I'll remember that your name is {name}." | |
| # Handle "remember that" requests to store facts | |
| if "remember that" in message.lower(): | |
| fact = message.lower().replace("remember that", "").strip() | |
| if fact: | |
| remember(fact, fact) # Save the fact in memory | |
| return f"Got it! I'll remember that: {fact}" | |
| else: | |
| return "You didn't provide anything to remember. Please say something after 'remember that'." | |
| # Default response from the model (if not asking about name) | |
| response = "" | |
| for message in client.chat_completion( | |
| messages, | |
| max_tokens=max_tokens, | |
| stream=True, | |
| temperature=temperature, | |
| top_p=top_p, | |
| ): | |
| token = message.choices[0].delta.content | |
| response += token | |
| yield response | |
| # Set up Gradio's chat interface | |
| demo = gr.ChatInterface( | |
| respond, | |
| additional_inputs=[ | |
| gr.Textbox(value="You are a friendly Chatbot.", label="System message"), | |
| gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"), | |
| gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"), | |
| gr.Slider( | |
| minimum=0.1, | |
| maximum=1.0, | |
| value=0.95, | |
| step=0.05, | |
| label="Top-p (nucleus sampling)", | |
| ), | |
| ], | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() | |
| # Model setup using transformers | |
| model_name = "APPenner/Aven" # Ensure this is the correct model path | |
| tokenizer = AutoTokenizer.from_pretrained(model_name) | |
| model = AutoModelForCausalLM.from_pretrained(model_name) | |
| # Logic for generating responses using the model | |
| def generate_response(prompt): | |
| inputs = tokenizer(prompt, return_tensors="pt") | |
| outputs = model.generate(**inputs) | |
| response = tokenizer.decode(outputs[0], skip_special_tokens=True) | |
| return response | |