Update app.py
Browse files
app.py
CHANGED
|
@@ -1,62 +1,65 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from langchain_huggingface import HuggingFacePipeline
|
| 3 |
-
from langchain.
|
| 4 |
-
from langchain_community.tools import DuckDuckGoSearchRun
|
| 5 |
from langchain.memory import ConversationBufferMemory
|
| 6 |
-
from
|
|
|
|
| 7 |
|
| 8 |
-
# Load
|
| 9 |
llm = HuggingFacePipeline.from_model_id(
|
| 10 |
-
model_id="
|
| 11 |
task="text-generation",
|
| 12 |
pipeline_kwargs={
|
| 13 |
-
"max_new_tokens":
|
| 14 |
"do_sample": True,
|
| 15 |
-
"temperature": 0.
|
| 16 |
-
"top_k":
|
| 17 |
-
"top_p": 0.
|
| 18 |
},
|
| 19 |
-
model_kwargs={"trust_remote_code": True}
|
| 20 |
)
|
| 21 |
|
| 22 |
-
#
|
|
|
|
|
|
|
|
|
|
| 23 |
search = DuckDuckGoSearchRun()
|
| 24 |
tools = [
|
| 25 |
Tool(
|
| 26 |
name="Web Search",
|
| 27 |
func=search.run,
|
| 28 |
-
description="
|
| 29 |
)
|
| 30 |
]
|
| 31 |
|
| 32 |
-
#
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
tools,
|
| 38 |
-
llm,
|
| 39 |
-
agent=AgentType.CONVERSATIONAL_REACT_DESCRIPTION, # Agent type that handles conversations and tools
|
| 40 |
-
verbose=True, # Logs reasoning (visible in console, not user-facing)
|
| 41 |
-
memory=memory # Enables context understanding across messages
|
| 42 |
)
|
| 43 |
|
| 44 |
-
#
|
| 45 |
def chat_with_agent(message, history):
|
| 46 |
try:
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
except Exception as e:
|
| 49 |
response = f"Error: {str(e)}. Try rephrasing your question."
|
| 50 |
return response
|
| 51 |
|
| 52 |
-
#
|
| 53 |
iface = gr.ChatInterface(
|
| 54 |
fn=chat_with_agent,
|
| 55 |
-
title="Free
|
| 56 |
-
description="A conversational AI
|
| 57 |
-
examples=["What's
|
| 58 |
)
|
| 59 |
|
| 60 |
-
# Launch the app (Hugging Face handles this automatically)
|
| 61 |
if __name__ == "__main__":
|
| 62 |
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from langchain_huggingface import HuggingFacePipeline
|
| 3 |
+
from langchain.chains import ConversationChain
|
|
|
|
| 4 |
from langchain.memory import ConversationBufferMemory
|
| 5 |
+
from langchain_community.tools import DuckDuckGoSearchRun
|
| 6 |
+
from langchain_core.tools import Tool
|
| 7 |
|
| 8 |
+
# Load a smaller, faster model (TinyLlama-1.1B-Chat, optimized for CPU)
|
| 9 |
llm = HuggingFacePipeline.from_model_id(
|
| 10 |
+
model_id="TinyLlama/TinyLlama-1.1B-Chat-v1.0",
|
| 11 |
task="text-generation",
|
| 12 |
pipeline_kwargs={
|
| 13 |
+
"max_new_tokens": 150, # Shorter responses for speed
|
| 14 |
"do_sample": True,
|
| 15 |
+
"temperature": 0.6, # Slightly less creative for consistency
|
| 16 |
+
"top_k": 40,
|
| 17 |
+
"top_p": 0.9
|
| 18 |
},
|
| 19 |
+
model_kwargs={"trust_remote_code": True}
|
| 20 |
)
|
| 21 |
|
| 22 |
+
# Set up conversation memory
|
| 23 |
+
memory = ConversationBufferMemory()
|
| 24 |
+
|
| 25 |
+
# Optional: Web search tool (comment out if you don’t need it for faster responses)
|
| 26 |
search = DuckDuckGoSearchRun()
|
| 27 |
tools = [
|
| 28 |
Tool(
|
| 29 |
name="Web Search",
|
| 30 |
func=search.run,
|
| 31 |
+
description="Use for current events or facts. Input a search query."
|
| 32 |
)
|
| 33 |
]
|
| 34 |
|
| 35 |
+
# Create a simple conversation chain (faster than full agent)
|
| 36 |
+
conversation = ConversationChain(
|
| 37 |
+
llm=llm,
|
| 38 |
+
memory=memory,
|
| 39 |
+
verbose=False # Disable logging for speed
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
)
|
| 41 |
|
| 42 |
+
# Function to handle chat and optional tool use
|
| 43 |
def chat_with_agent(message, history):
|
| 44 |
try:
|
| 45 |
+
# Check if the query likely needs a web search (e.g., current events)
|
| 46 |
+
if any(keyword in message.lower() for keyword in ["latest", "news", "current", "today", "weather"]):
|
| 47 |
+
search_result = search.run(message)
|
| 48 |
+
prompt = f"User asked: {message}\nWeb search result: {search_result}\nAnswer based on this info."
|
| 49 |
+
response = conversation.predict(input=prompt)
|
| 50 |
+
else:
|
| 51 |
+
response = conversation.predict(input=message)
|
| 52 |
except Exception as e:
|
| 53 |
response = f"Error: {str(e)}. Try rephrasing your question."
|
| 54 |
return response
|
| 55 |
|
| 56 |
+
# Gradio chat interface
|
| 57 |
iface = gr.ChatInterface(
|
| 58 |
fn=chat_with_agent,
|
| 59 |
+
title="Fast Free AI Agent",
|
| 60 |
+
description="A lightweight conversational AI that remembers our talks and can search the web. Hosted free on Hugging Face Spaces. Responses should be faster (5-15 seconds).",
|
| 61 |
+
examples=["What's my name if I told you it's Alex?", "Tell me a quick joke.", "What's the latest AI news?"]
|
| 62 |
)
|
| 63 |
|
|
|
|
| 64 |
if __name__ == "__main__":
|
| 65 |
iface.launch()
|