Spaces:
Running
Running
Upload app.py with huggingface_hub
Browse files
app.py
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
|
| 5 |
+
# 1. System Prompt/Personality for Hermes Agent
|
| 6 |
+
HERMES_SYSTEM_PROMPT = """You are Hermes Agent, a helpful, fast, and practical multi-purpose assistant. You are professional, calm, and user-friendly. You can answer questions, reason through tasks step-by-step, plan, summarize, provide coding help, offer research-style explanations, and break down complex tasks. Do not pretend to have abilities you do not possess. Always strive for clarity and conciseness."""
|
| 7 |
+
|
| 8 |
+
# 2. Model Integration
|
| 9 |
+
# Using a small, CPU-friendly model for demonstration on Hugging Face Spaces free tier.
|
| 10 |
+
# For better performance and more complex tasks, a larger model with GPU would be recommended.
|
| 11 |
+
# Example: 'distilgpt2' is a good starting point for CPU inference.
|
| 12 |
+
# For more capable models, consider 'HuggingFaceH4/zephyr-7b-beta' or 'mistralai/Mistral-7B-Instruct-v0.2'
|
| 13 |
+
# which would require a GPU-enabled Space.
|
| 14 |
+
|
| 15 |
+
try:
|
| 16 |
+
# Initialize the pipeline for text generation
|
| 17 |
+
# Using 'text-generation' task with a pre-trained model
|
| 18 |
+
# Setting trust_remote_code=True might be necessary for some models, but generally avoid if not explicitly needed.
|
| 19 |
+
generator = pipeline('text-generation', model='distilgpt2')
|
| 20 |
+
except Exception as e:
|
| 21 |
+
print(f"Error loading model: {e}")
|
| 22 |
+
generator = None
|
| 23 |
+
|
| 24 |
+
# 3. Chatbot Logic
|
| 25 |
+
def predict(message, history):
|
| 26 |
+
if generator is None:
|
| 27 |
+
return "Error: Model could not be loaded. Please check the backend logs."
|
| 28 |
+
|
| 29 |
+
# Format conversation history for the model
|
| 30 |
+
# For distilgpt2, a simple concatenation is sufficient.
|
| 31 |
+
# For more advanced models, a specific chat template might be required.
|
| 32 |
+
conversation = HERMES_SYSTEM_PROMPT + "\n\n"
|
| 33 |
+
for human, agent in history:
|
| 34 |
+
conversation += f"User: {human}\nHermes Agent: {agent}\n"
|
| 35 |
+
conversation += f"User: {message}\nHermes Agent:"
|
| 36 |
+
|
| 37 |
+
try:
|
| 38 |
+
# Generate response
|
| 39 |
+
# max_new_tokens controls the length of the generated response
|
| 40 |
+
# num_return_sequences=1 to get a single best response
|
| 41 |
+
# truncation=True to handle long inputs gracefully
|
| 42 |
+
response = generator(conversation, max_new_tokens=150, num_return_sequences=1, truncation=True)
|
| 43 |
+
generated_text = response[0]['generated_text']
|
| 44 |
+
|
| 45 |
+
# Extract only the agent's response, removing the prompt and user's input
|
| 46 |
+
# This is a simple heuristic and might need refinement for complex models/prompts
|
| 47 |
+
agent_response_start = generated_text.rfind("Hermes Agent:")
|
| 48 |
+
if agent_response_start != -1:
|
| 49 |
+
agent_response = generated_text[agent_response_start + len("Hermes Agent:"):].strip()
|
| 50 |
+
else:
|
| 51 |
+
agent_response = generated_text.strip() # Fallback if marker not found
|
| 52 |
+
|
| 53 |
+
# Clean up any potential incomplete sentences or model artifacts
|
| 54 |
+
# For distilgpt2, it often generates incomplete sentences, so we might need to truncate at the last punctuation.
|
| 55 |
+
last_punctuation = max(agent_response.rfind('.'), agent_response.rfind('?'), agent_response.rfind('!'))
|
| 56 |
+
if last_punctuation != -1:
|
| 57 |
+
agent_response = agent_response[:last_punctuation + 1]
|
| 58 |
+
|
| 59 |
+
return agent_response
|
| 60 |
+
|
| 61 |
+
except Exception as e:
|
| 62 |
+
return f"An error occurred during model inference: {e}"
|
| 63 |
+
|
| 64 |
+
# 4. Gradio Web UI
|
| 65 |
+
with gr.Blocks() as demo:
|
| 66 |
+
gr.Markdown("# Hermes Agent")
|
| 67 |
+
gr.Markdown("""
|
| 68 |
+
Hermes Agent is a helpful, fast, and practical multi-purpose AI assistant.
|
| 69 |
+
It can answer questions, reason through tasks, plan, summarize, and provide coding help.
|
| 70 |
+
""")
|
| 71 |
+
|
| 72 |
+
chatbot = gr.Chatbot(height=400)
|
| 73 |
+
msg = gr.Textbox(label="Your Message", placeholder="Type your message here...")
|
| 74 |
+
clear = gr.Button("Clear")
|
| 75 |
+
|
| 76 |
+
msg.submit(predict, [msg, chatbot], [msg, chatbot])
|
| 77 |
+
clear.click(lambda: None, None, [msg, chatbot], queue=False)
|
| 78 |
+
|
| 79 |
+
# Launch the Gradio app
|
| 80 |
+
# The share=True option creates a public link, useful for testing, but should be False for deployment on Spaces.
|
| 81 |
+
# For Hugging Face Spaces, the app runs automatically when app.py is present.
|
| 82 |
+
if __name__ == "__main__":
|
| 83 |
+
demo.launch(debug=True) # debug=True for local development, set to False for production
|