apaxray commited on
Commit
b2cc17d
·
verified ·
1 Parent(s): eee029e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -47
app.py CHANGED
@@ -1,47 +1,38 @@
1
- from transformers import AutoTokenizer, AutoModelForCausalLM
2
-
3
- import os
4
-
5
-
6
- # دسترسی به توکن از Secrets در Hugging Face
7
- token = os.getenv("Passsssssss")
8
-
9
-
10
- # Load model directly
11
- from transformers import AutoTokenizer, AutoModelForCausalLM
12
-
13
- tokenizer = AutoTokenizer.from_pretrained("tiiuae/falcon-7b-instruct", trust_remote_code=True)
14
- model = AutoModelForCausalLM.from_pretrained("tiiuae/falcon-7b-instruct", trust_remote_code=True)
15
-
16
-
17
- # مدل‌های مختلف AI
18
- models = {
19
- 'falcon': "huggingface/falcon-7b-instruct",
20
- 'gpt_neox': "EleutherAI/gpt-neox-20b",
21
- 'persian': "HooshvareLab/bert-fa-zwnj-base",
22
- 'math': "EleutherAI/gpt-neox-20b-math" # مدل ریاضی (باید ایجاد شود یا از مدل‌های مشابه استفاده کنید)
23
- }
24
-
25
- # بارگذاری مدل‌ها از Hugging Face
26
- tokenizers = {name: AutoTokenizer.from_pretrained(path) for name, path in models.items()}
27
- models = {name: AutoModelForCausalLM.from_pretrained(path) for name, path in models.items()}
28
-
29
- def generate_response(prompt, model_name="falcon"):
30
- tokenizer = tokenizers[model_name]
31
- model = models[model_name]
32
-
33
- inputs = tokenizer(prompt, return_tensors="pt")
34
- outputs = model.generate(inputs["input_ids"], max_length=100, num_return_sequences=1)
35
- response = tokenizer.decode(outputs[0], skip_special_tokens=True)
36
-
37
- return response
38
-
39
- # نمونه استفاده
40
- prompt = "سلام، امروز چه خبر؟"
41
- response_falcon = generate_response(prompt, model_name="falcon")
42
- response_gpt_neox = generate_response(prompt, model_name="gpt_neox")
43
- response_persian = generate_response(prompt, model_name="persian")
44
-
45
- print("Falcon Response:", response_falcon)
46
- print("GPT-NeoX Response:", response_gpt_neox)
47
- print("Persian Response:", response_persian)
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+ import jdatetime
4
+ from sympy import sympify
5
+
6
+ # مدل پرسش و پاسخ فارسی
7
+ qa_pipeline = pipeline("question-answering", model="Hooshvare/bert-fa-base-uncased", tokenizer="Hooshvare/bert-fa-base-uncased")
8
+
9
+ # توابع اصلی
10
+ def process_text(input_text):
11
+ # تحلیل سوال
12
+ if "تاریخ" in input_text:
13
+ today = jdatetime.date.today()
14
+ return f"امروز: {today}"
15
+ elif "محاسبه" in input_text:
16
+ expression = input_text.replace("محاسبه", "").strip()
17
+ try:
18
+ result = sympify(expression)
19
+ return f"نتیجه محاسبه: {result}"
20
+ except Exception as e:
21
+ return f"خطا در محاسبه: {e}"
22
+ else:
23
+ # پاسخ به سوال
24
+ context = "اینجا متنی برای مثال قرار دارد که هوش مصنوعی می‌تواند از آن پاسخ بدهد."
25
+ response = qa_pipeline({"question": input_text, "context": context})
26
+ return f"پاسخ: {response['answer']}"
27
+
28
+ # رابط کاربری Gradio
29
+ interface = gr.Interface(
30
+ fn=process_text,
31
+ inputs=gr.Textbox(lines=5, placeholder="سوال یا متن خود را وارد کنید..."),
32
+ outputs="text",
33
+ title="هوش مصنوعی فارسی",
34
+ description="پرسش‌های خود را وارد کنید، محاسبات انجام دهید یا تاریخ امروز را دریافت کنید.",
35
+ )
36
+
37
+ if __name__ == "__main__":
38
+ interface.launch()