Spaces:
Runtime error
Runtime error
Create withoutllama
Browse files- withoutllama +113 -0
withoutllama
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from langchain_community.tools import WikipediaQueryRun, ArxivQueryRun
|
| 3 |
+
from langchain_community.utilities import WikipediaAPIWrapper, ArxivAPIWrapper
|
| 4 |
+
from langchain_huggingface import HuggingFacePipeline
|
| 5 |
+
from langchain.agents import initialize_agent, AgentType
|
| 6 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, TextGenerationPipeline
|
| 7 |
+
from huggingface_hub import login
|
| 8 |
+
import torch
|
| 9 |
+
import traceback
|
| 10 |
+
|
| 11 |
+
# β
Login to HF
|
| 12 |
+
token = os.getenv("HF_TOKEN")
|
| 13 |
+
print("π HF_TOKEN available?", token is not None)
|
| 14 |
+
if token:
|
| 15 |
+
login(token=token)
|
| 16 |
+
else:
|
| 17 |
+
print("β No HF_TOKEN found in environment")
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def build_qa():
|
| 21 |
+
print("π Starting QA pipeline...")
|
| 22 |
+
|
| 23 |
+
# ---- 1. Tools ----
|
| 24 |
+
try:
|
| 25 |
+
print("πΉ Initializing Wikipedia tool...")
|
| 26 |
+
wiki_wrapper = WikipediaAPIWrapper(top_k_results=1, doc_content_chars_max=200)
|
| 27 |
+
wiki = WikipediaQueryRun(api_wrapper=wiki_wrapper)
|
| 28 |
+
|
| 29 |
+
print("πΉ Initializing Arxiv tool...")
|
| 30 |
+
arxiv_wrapper = ArxivAPIWrapper(top_k_results=1, doc_content_chars_max=200)
|
| 31 |
+
arxiv = ArxivQueryRun(api_wrapper=arxiv_wrapper)
|
| 32 |
+
|
| 33 |
+
tools = [wiki, arxiv]
|
| 34 |
+
print("β
Tools initialized")
|
| 35 |
+
except Exception as e:
|
| 36 |
+
print("β Tools initialization failed:", e)
|
| 37 |
+
traceback.print_exc()
|
| 38 |
+
return None
|
| 39 |
+
|
| 40 |
+
# ---- 2. Model ----
|
| 41 |
+
try:
|
| 42 |
+
print("πΉ Loading Mistral 7B model...")
|
| 43 |
+
model_name = "mistralai/Mistral-7B-Instruct-v0.3" # or your CPU-quantized version
|
| 44 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 45 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 46 |
+
model_name,
|
| 47 |
+
device_map="auto", # works after installing accelerate
|
| 48 |
+
dtype=torch.float16, # instead of torch_dtype
|
| 49 |
+
)
|
| 50 |
+
llm = TextGenerationPipeline(
|
| 51 |
+
model=model,
|
| 52 |
+
tokenizer=tokenizer,
|
| 53 |
+
max_new_tokens=256,
|
| 54 |
+
temperature=0.2,
|
| 55 |
+
do_sample=False,
|
| 56 |
+
top_p=0.9,
|
| 57 |
+
repetition_penalty=1.2,
|
| 58 |
+
eos_token_id=tokenizer.eos_token_id,
|
| 59 |
+
return_full_text=False,
|
| 60 |
+
)
|
| 61 |
+
hf_llm = HuggingFacePipeline(pipeline=llm)
|
| 62 |
+
print(f"β
Model loaded: {model_name}")
|
| 63 |
+
except Exception as e:
|
| 64 |
+
print("β Model load failed:", e)
|
| 65 |
+
traceback.print_exc()
|
| 66 |
+
return None
|
| 67 |
+
|
| 68 |
+
# ---- 3. Agent ----
|
| 69 |
+
try:
|
| 70 |
+
print("πΉ Initializing agent...")
|
| 71 |
+
agent = initialize_agent(
|
| 72 |
+
tools=tools,
|
| 73 |
+
llm=hf_llm,
|
| 74 |
+
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
|
| 75 |
+
verbose=True,
|
| 76 |
+
handle_parsing_errors=True,
|
| 77 |
+
)
|
| 78 |
+
print("β
Agent initialized")
|
| 79 |
+
except Exception as e:
|
| 80 |
+
print("β Agent initialization failed:", e)
|
| 81 |
+
traceback.print_exc()
|
| 82 |
+
return None
|
| 83 |
+
|
| 84 |
+
print("β
QA pipeline ready")
|
| 85 |
+
return agent
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
# ---- Build once ----
|
| 89 |
+
try:
|
| 90 |
+
agent = build_qa()
|
| 91 |
+
if agent:
|
| 92 |
+
print("β
QA pipeline built successfully:", type(agent))
|
| 93 |
+
else:
|
| 94 |
+
print("β QA pipeline build returned None")
|
| 95 |
+
except Exception as e:
|
| 96 |
+
agent = None
|
| 97 |
+
print("β Failed to build QA pipeline:", e)
|
| 98 |
+
traceback.print_exc()
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
def get_response(user_message, history):
|
| 102 |
+
if agent is None:
|
| 103 |
+
return "β οΈ QA pipeline not initialized."
|
| 104 |
+
|
| 105 |
+
try:
|
| 106 |
+
print("π¬ User query:", user_message)
|
| 107 |
+
response = agent.invoke({"input": user_message})
|
| 108 |
+
print("π€ Agent response:", response)
|
| 109 |
+
return response
|
| 110 |
+
except Exception as e:
|
| 111 |
+
print("β Agent execution failed:", e)
|
| 112 |
+
traceback.print_exc()
|
| 113 |
+
return f"β QA run failed: {e}"
|