duduvicky commited on
Commit
f7b2e6e
·
verified ·
1 Parent(s): 5e5a508

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -29
app.py CHANGED
@@ -1,62 +1,65 @@
1
  import gradio as gr
2
  from langchain_huggingface import HuggingFacePipeline
3
- from langchain.agents import initialize_agent, Tool
4
- from langchain_community.tools import DuckDuckGoSearchRun
5
  from langchain.memory import ConversationBufferMemory
6
- from langchain.agents import AgentType
 
7
 
8
- # Load the free open-source LLM (Phi-2 runs on CPU, might be a bit slow but works for free hosting)
9
  llm = HuggingFacePipeline.from_model_id(
10
- model_id="microsoft/phi-2",
11
  task="text-generation",
12
  pipeline_kwargs={
13
- "max_new_tokens": 256, # Limit response length
14
  "do_sample": True,
15
- "temperature": 0.7, # Creativity level
16
- "top_k": 50,
17
- "top_p": 0.95
18
  },
19
- model_kwargs={"trust_remote_code": True} # Required for Phi-2
20
  )
21
 
22
- # Define tools the agent can use (e.g., web search for current info)
 
 
 
23
  search = DuckDuckGoSearchRun()
24
  tools = [
25
  Tool(
26
  name="Web Search",
27
  func=search.run,
28
- description="Useful for answering questions about current events, facts, or anything requiring up-to-date web information. Input should be a search query."
29
  )
30
  ]
31
 
32
- # Set up memory to remember conversation history
33
- memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
34
-
35
- # Initialize the conversational agent
36
- agent = initialize_agent(
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
- # Gradio chat function: Handles user input and gets agent response
45
  def chat_with_agent(message, history):
46
  try:
47
- response = agent.invoke({"input": message})["output"]
 
 
 
 
 
 
48
  except Exception as e:
49
  response = f"Error: {str(e)}. Try rephrasing your question."
50
  return response
51
 
52
- # Create the Gradio chat interface
53
  iface = gr.ChatInterface(
54
  fn=chat_with_agent,
55
- title="Free Cloud AI Agent",
56
- description="A conversational AI agent that remembers our talks and can search the web for info. Powered by Phi-2, LangChain, and hosted free on Hugging Face Spaces. Responses may take 10-30 seconds on free CPU.",
57
- examples=["What's the latest news on AI?", "Tell me a joke.", "Remember my name is Alex. What's my name?"] # Demo prompts
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()