Update app.py
Browse files
app.py
CHANGED
|
@@ -45,6 +45,43 @@ from smolagents.gradio_ui import pull_messages_from_step, handle_agent_output_ty
|
|
| 45 |
|
| 46 |
from smolagents import Tool
|
| 47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
class GoogleSearchTool(Tool):
|
| 50 |
name = "web_search"
|
|
@@ -239,9 +276,11 @@ def stream_to_gradio(
|
|
| 239 |
final_answer = handle_agent_output_types(final_answer)
|
| 240 |
|
| 241 |
if isinstance(final_answer, AgentText):
|
|
|
|
|
|
|
| 242 |
yield gr.ChatMessage(
|
| 243 |
role="assistant",
|
| 244 |
-
content=f"**Final answer:**\n{final_answer.to_string()}\n",
|
| 245 |
)
|
| 246 |
elif isinstance(final_answer, AgentImage):
|
| 247 |
yield gr.ChatMessage(
|
|
|
|
| 45 |
|
| 46 |
from smolagents import Tool
|
| 47 |
|
| 48 |
+
from huggingface_hub import InferenceClient
|
| 49 |
+
def hf_chat(api_key, model, text):
|
| 50 |
+
client = InferenceClient(api_key=api_key)
|
| 51 |
+
messages = [
|
| 52 |
+
{
|
| 53 |
+
"role": "user",
|
| 54 |
+
"content": text,
|
| 55 |
+
}
|
| 56 |
+
]
|
| 57 |
+
|
| 58 |
+
stream = client.chat.completions.create(
|
| 59 |
+
model=model, messages=messages, max_tokens=6000, stream=False
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
return stream.choices[0].message.content
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
import openai
|
| 67 |
+
|
| 68 |
+
openai.api_key = os.getenv("HF_TOKEN")
|
| 69 |
+
openai.api_base = "YOUR_API_ENDPOINT" # 例: "https://your-api-provider.com/v1"
|
| 70 |
+
model_name = "YOUR_MODEL_NAME" # 例: "my-custom-model"
|
| 71 |
+
|
| 72 |
+
def generate_text(prompt):
|
| 73 |
+
try:
|
| 74 |
+
response = openai.Completion.create(
|
| 75 |
+
engine=model_name,
|
| 76 |
+
prompt=prompt,
|
| 77 |
+
n=1,
|
| 78 |
+
stop=None,
|
| 79 |
+
temperature=0.9 # ランダム性 (0: deterministic, 1: creative)
|
| 80 |
+
)
|
| 81 |
+
return response.choices[0].text.strip()
|
| 82 |
+
except Exception as e:
|
| 83 |
+
print(f"Error happend: {e}")
|
| 84 |
+
return None
|
| 85 |
|
| 86 |
class GoogleSearchTool(Tool):
|
| 87 |
name = "web_search"
|
|
|
|
| 276 |
final_answer = handle_agent_output_types(final_answer)
|
| 277 |
|
| 278 |
if isinstance(final_answer, AgentText):
|
| 279 |
+
jp=hf_chat(None,"google/gemma-2-27b-it",f"以下を日本語に翻訳して:{final_answer.to_string()}")
|
| 280 |
+
|
| 281 |
yield gr.ChatMessage(
|
| 282 |
role="assistant",
|
| 283 |
+
content=f"**Final answer:**\n{final_answer.to_string()}\n+日本語:\n{jp}",
|
| 284 |
)
|
| 285 |
elif isinstance(final_answer, AgentImage):
|
| 286 |
yield gr.ChatMessage(
|