Spaces:
Sleeping
Sleeping
Update agent.py
Browse files
agent.py
CHANGED
|
@@ -1,37 +1,25 @@
|
|
| 1 |
-
|
| 2 |
-
from openai import OpenAI
|
| 3 |
-
from tools.file_loader import load_file_if_any
|
| 4 |
|
| 5 |
-
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
Only return the answer itself — no explanations or formatting."""
|
| 10 |
|
| 11 |
-
def answer_question(
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
if question_obj.get("file_path"):
|
| 15 |
-
file_content = load_file_if_any(question_obj["file_path"])
|
| 16 |
-
|
| 17 |
-
user_prompt = f"""Question:
|
| 18 |
-
{question_obj['question']}
|
| 19 |
-
|
| 20 |
-
Attached file content (if any):
|
| 21 |
-
{file_content}
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
model="gpt-3.5-turbo",
|
| 28 |
-
messages=[
|
| 29 |
-
{"role": "system", "content": SYSTEM_PROMPT},
|
| 30 |
-
{"role": "user", "content": user_prompt}
|
| 31 |
-
]
|
| 32 |
-
)
|
| 33 |
-
|
| 34 |
-
return response.choices[0].message.content.strip()
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
except Exception as e:
|
| 37 |
-
return f"[ERROR]
|
|
|
|
| 1 |
+
# agent.py (Updated for Hugging Face inference with FLAN-T5)
|
|
|
|
|
|
|
| 2 |
|
| 3 |
+
from transformers import pipeline
|
| 4 |
|
| 5 |
+
# Load the Hugging Face inference pipeline
|
| 6 |
+
qa_pipeline = pipeline("text2text-generation", model="google/flan-t5-base")
|
|
|
|
| 7 |
|
| 8 |
+
def answer_question(question: str, file_context: str = None, do_search: bool = False) -> str:
|
| 9 |
+
"""
|
| 10 |
+
Answers the question using file context (if any) with a Hugging Face model.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
Args:
|
| 13 |
+
question (str): The question to be answered.
|
| 14 |
+
file_context (str, optional): Optional context extracted from a file.
|
| 15 |
+
do_search (bool): Ignored for local mode.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
Returns:
|
| 18 |
+
str: The generated answer.
|
| 19 |
+
"""
|
| 20 |
+
prompt = question if not file_context else f"Context: {file_context}\nQuestion: {question}"
|
| 21 |
+
try:
|
| 22 |
+
result = qa_pipeline(prompt, max_length=256, do_sample=False)
|
| 23 |
+
return result[0]["generated_text"].strip()
|
| 24 |
except Exception as e:
|
| 25 |
+
return f"[ERROR] Hugging Face pipeline failed: {e}"
|