jarguello76 commited on
Commit
ad04fb9
·
verified ·
1 Parent(s): 48264e3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -6
app.py CHANGED
@@ -1,11 +1,56 @@
1
  import os
2
- from huggingface_hub import InferenceApi
 
 
 
 
3
 
4
- inference_api = InferenceApi(
5
- repo_id="Qwen/Qwen1.5-1.8B-Chat",
6
- token=os.environ["HUGGINGFACE_API_KEY"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  )
8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
- llm = HuggingFaceInferenceWrapper(inference_api)
11
- print(llm.generate("What is the capital of France?"))
 
 
 
1
  import os
2
+ from fastapi import FastAPI
3
+ from pydantic import BaseModel
4
+ from huggingface_hub import InferenceClient
5
+ from smolagent import BasicAgent
6
+ from crewai_tools import BaseTool
7
 
8
+ # Use your token from HF secrets
9
+ HF_TOKEN = os.environ.get("HF_TOKEN") # Must be set in HF Space secrets
10
+
11
+ # Define a wrapper that conforms to SmolAgent’s LLM expectations
12
+ class HuggingFaceInferenceWrapper:
13
+ def __init__(self, client: InferenceClient):
14
+ self.client = client
15
+
16
+ def __call__(self, prompt: str, **kwargs):
17
+ # Use text_generation endpoint
18
+ response = self.client.text_generation(prompt=prompt, max_new_tokens=512)
19
+ return response
20
+
21
+ # Initialize InferenceClient
22
+ client = InferenceClient(
23
+ model="Qwen/Qwen1.5-1.8B-Chat", # Or another model you have access to
24
+ token=HF_TOKEN
25
  )
26
 
27
+ # Wrap the client
28
+ llm = HuggingFaceInferenceWrapper(client)
29
+
30
+ # Define a dummy tool
31
+ class DuckDuckGoTool(BaseTool):
32
+ name: str = "DuckDuckGo Search"
33
+ description: str = "Use this tool to search for real-time facts and current events."
34
+
35
+ def _run(self, query: str) -> str:
36
+ return f"Search results for '{query}' (this is a placeholder)."
37
+
38
+ # Create the agent
39
+ agent = BasicAgent(
40
+ role="Helpful AI",
41
+ goal="Answer tough questions using search and reasoning",
42
+ backstory="An expert at finding information and answering questions.",
43
+ tools=[DuckDuckGoTool()],
44
+ llm=llm
45
+ )
46
+
47
+ # Define FastAPI app
48
+ app = FastAPI()
49
+
50
+ class QuestionInput(BaseModel):
51
+ question: str
52
 
53
+ @app.post("/ask")
54
+ def ask_question(payload: QuestionInput):
55
+ result = agent.run(payload.question)
56
+ return {"answer": result}