Update app.py
Browse files
app.py
CHANGED
|
@@ -3,7 +3,7 @@ from langchain_huggingface import HuggingFacePipeline
|
|
| 3 |
from langchain.chains import ConversationChain
|
| 4 |
from langchain.memory import ConversationBufferMemory
|
| 5 |
|
| 6 |
-
# Load
|
| 7 |
llm = HuggingFacePipeline.from_model_id(
|
| 8 |
model_id="distilgpt2",
|
| 9 |
task="text-generation",
|
|
@@ -26,6 +26,11 @@ conversation = ConversationChain(
|
|
| 26 |
verbose=False # No logging for speed
|
| 27 |
)
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
# Chat function
|
| 30 |
def chat_with_agent(message, history):
|
| 31 |
try:
|
|
@@ -37,19 +42,20 @@ def chat_with_agent(message, history):
|
|
| 37 |
return response
|
| 38 |
|
| 39 |
# Gradio chat interface
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
|
|
|
| 54 |
if __name__ == "__main__":
|
| 55 |
iface.launch()
|
|
|
|
| 3 |
from langchain.chains import ConversationChain
|
| 4 |
from langchain.memory import ConversationBufferMemory
|
| 5 |
|
| 6 |
+
# Load lightweight model (distilgpt2, ~82M parameters, fast on CPU)
|
| 7 |
llm = HuggingFacePipeline.from_model_id(
|
| 8 |
model_id="distilgpt2",
|
| 9 |
task="text-generation",
|
|
|
|
| 26 |
verbose=False # No logging for speed
|
| 27 |
)
|
| 28 |
|
| 29 |
+
# Clear memory function (called via a separate button in the UI)
|
| 30 |
+
def clear_memory():
|
| 31 |
+
memory.clear()
|
| 32 |
+
return "Conversation history cleared!"
|
| 33 |
+
|
| 34 |
# Chat function
|
| 35 |
def chat_with_agent(message, history):
|
| 36 |
try:
|
|
|
|
| 42 |
return response
|
| 43 |
|
| 44 |
# Gradio chat interface
|
| 45 |
+
with gr.Blocks() as iface:
|
| 46 |
+
gr.Markdown("# Fast Free AI Agent")
|
| 47 |
+
gr.Markdown("A lightweight conversational AI that remembers our talks. Hosted free on Hugging Face Spaces. Responses in ~3-10 seconds.")
|
| 48 |
+
chatbot = gr.ChatInterface(
|
| 49 |
+
fn=chat_with_agent,
|
| 50 |
+
examples=[
|
| 51 |
+
{"text": "My name is Alex. What's my name?"},
|
| 52 |
+
{"text": "Tell me a short joke."}
|
| 53 |
+
],
|
| 54 |
+
title="Chat with Your AI Agent",
|
| 55 |
+
description="Type your message below to chat. The AI remembers our conversation!"
|
| 56 |
+
)
|
| 57 |
+
gr.Button("Clear Conversation History").click(fn=clear_memory, outputs=gr.Textbox())
|
| 58 |
+
|
| 59 |
+
# Launch the app
|
| 60 |
if __name__ == "__main__":
|
| 61 |
iface.launch()
|