Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,62 +1,44 @@
|
|
1 |
-
import gradio as gr
|
2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
|
|
|
|
|
|
3 |
|
4 |
-
#
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
|
9 |
-
#
|
10 |
-
|
11 |
-
|
12 |
-
history: list[tuple[str, str]],
|
13 |
-
system_message: str,
|
14 |
-
max_tokens: int,
|
15 |
-
temperature: float,
|
16 |
-
top_p: float,
|
17 |
-
):
|
18 |
-
# ساختن prompt از پیامهای قبلی
|
19 |
-
context = f"{system_message}\n"
|
20 |
-
for user_message, bot_response in history:
|
21 |
-
context += f"User: {user_message}\nBot: {bot_response}\n"
|
22 |
-
context += f"User: {message}\nBot:"
|
23 |
|
24 |
-
|
25 |
-
|
26 |
-
outputs = model.generate(
|
27 |
-
inputs["input_ids"],
|
28 |
-
max_new_tokens=max_tokens,
|
29 |
-
temperature=temperature,
|
30 |
-
top_p=top_p,
|
31 |
-
pad_token_id=tokenizer.eos_token_id,
|
32 |
-
)
|
33 |
-
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
34 |
-
response = response.split("Bot:")[-1].strip() # استخراج پاسخ
|
35 |
|
36 |
-
|
|
|
37 |
|
|
|
|
|
|
|
|
|
38 |
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
)
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
),
|
56 |
-
],
|
57 |
-
title="Advanced Chatbot with Llama 2",
|
58 |
-
description="A conversational AI based on Llama 2 fine-tuned for chat.",
|
59 |
-
)
|
60 |
|
61 |
if __name__ == "__main__":
|
62 |
-
|
|
|
|
|
1 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
2 |
+
from sympy import solve, symbols
|
3 |
+
from fastapi import FastAPI
|
4 |
+
import uvicorn
|
5 |
|
6 |
+
# مدلهای مختلف
|
7 |
+
MODEL_GENERAL = "meta-llama/Llama-2-7b-chat-hf"
|
8 |
+
MODEL_IRAN = "HooshvareLab/bert-fa-base-uncased"
|
9 |
+
MODEL_MATH = None # SymPy برای ریاضی
|
10 |
|
11 |
+
# بارگذاری مدلها
|
12 |
+
tokenizer_general = AutoTokenizer.from_pretrained(MODEL_GENERAL)
|
13 |
+
model_general = AutoModelForCausalLM.from_pretrained(MODEL_GENERAL)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
+
tokenizer_iran = AutoTokenizer.from_pretrained(MODEL_IRAN)
|
16 |
+
model_iran = AutoModelForCausalLM.from_pretrained(MODEL_IRAN)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
+
# FastAPI برای مدیریت درخواستها
|
19 |
+
app = FastAPI()
|
20 |
|
21 |
+
def generate_response(model, tokenizer, prompt, max_tokens=100):
|
22 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
23 |
+
outputs = model.generate(inputs.input_ids, max_new_tokens=max_tokens)
|
24 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
25 |
|
26 |
+
@app.post("/chat")
|
27 |
+
def chat(input_text: str, mode: str = "general"):
|
28 |
+
if mode == "general":
|
29 |
+
response = generate_response(model_general, tokenizer_general, input_text)
|
30 |
+
elif mode == "iran":
|
31 |
+
response = generate_response(model_iran, tokenizer_iran, input_text)
|
32 |
+
elif mode == "math":
|
33 |
+
x = symbols("x")
|
34 |
+
try:
|
35 |
+
solution = solve(input_text, x)
|
36 |
+
response = f"Solution: {solution}"
|
37 |
+
except Exception as e:
|
38 |
+
response = f"Math error: {str(e)}"
|
39 |
+
else:
|
40 |
+
response = "Invalid mode selected."
|
41 |
+
return {"response": response}
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
if __name__ == "__main__":
|
44 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|