apaxray commited on
Commit
43454ac
·
verified ·
1 Parent(s): d639fa2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -53
app.py CHANGED
@@ -1,62 +1,44 @@
1
- import gradio as gr
2
  from transformers import AutoModelForCausalLM, AutoTokenizer
 
 
 
3
 
4
- # بارگذاری مدل و توکنایزر از Hugging Face
5
- MODEL_NAME = "meta-llama/Llama-2-7b-chat-hf" # مدل Llama 2 (نسخه Chat)
6
- tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
7
- model = AutoModelForCausalLM.from_pretrained(MODEL_NAME, device_map="auto")
8
 
9
- # تعریف تابع پاسخ‌دهی
10
- def respond(
11
- message: str,
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
- inputs = tokenizer(context, return_tensors="pt", padding=True, truncation=True)
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
- yield response
 
37
 
 
 
 
 
38
 
39
- # رابط کاربری Gradio
40
- demo = gr.ChatInterface(
41
- fn=respond,
42
- additional_inputs=[
43
- gr.Textbox(
44
- value="You are an advanced and friendly assistant.",
45
- label="System message",
46
- ),
47
- gr.Slider(
48
- minimum=10, maximum=1024, value=256, step=1, label="Max new tokens"
49
- ),
50
- gr.Slider(
51
- minimum=0.1, maximum=2.0, value=0.7, step=0.1, label="Temperature"
52
- ),
53
- gr.Slider(
54
- minimum=0.1, maximum=1.0, value=0.9, step=0.05, label="Top-p"
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
- demo.launch()
 
 
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)