Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,27 +1,156 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
history = history or []
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
| 12 |
else:
|
| 13 |
-
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
with gr.Blocks() as demo:
|
| 19 |
-
gr.Markdown("#
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
|
| 25 |
-
clear.click(lambda: None, None, chatbot, queue=False)
|
| 26 |
|
| 27 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
import re
|
| 4 |
+
import time
|
| 5 |
+
import torch
|
| 6 |
+
import tempfile
|
| 7 |
+
from datetime import datetime
|
| 8 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 9 |
+
from gtts import gTTS
|
| 10 |
+
from PIL import Image
|
| 11 |
+
from diffusers import StableDiffusionPipeline
|
| 12 |
+
from PyPDF2 import PdfReader
|
| 13 |
+
import speech_recognition as sr
|
| 14 |
|
| 15 |
+
# ================== تنظیمات اولیه ==================
|
| 16 |
+
|
| 17 |
+
os.makedirs("conversations", exist_ok=True) # پوشه آرشیو گفتگوها
|
| 18 |
+
|
| 19 |
+
en_model_name = "HuggingFaceH4/zephyr-7b-beta"
|
| 20 |
+
fa_model_name = "HooshvareLab/gpt2-fa"
|
| 21 |
+
|
| 22 |
+
# بارگذاری مدلها
|
| 23 |
+
en_tokenizer = AutoTokenizer.from_pretrained(en_model_name)
|
| 24 |
+
en_model = AutoModelForCausalLM.from_pretrained(en_model_name, torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32)
|
| 25 |
+
en_model.eval()
|
| 26 |
+
|
| 27 |
+
fa_tokenizer = AutoTokenizer.from_pretrained(fa_model_name)
|
| 28 |
+
fa_model = AutoModelForCausalLM.from_pretrained(fa_model_name)
|
| 29 |
+
fa_model.eval()
|
| 30 |
+
|
| 31 |
+
# بارگذاری مدل تولید تصویر
|
| 32 |
+
image_pipe = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4", torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32)
|
| 33 |
+
image_pipe = image_pipe.to("cuda" if torch.cuda.is_available() else "cpu")
|
| 34 |
+
|
| 35 |
+
# ================== توابع کمکی ==================
|
| 36 |
+
|
| 37 |
+
def detect_language(text):
|
| 38 |
+
return bool(re.search(r'[\u0600-\u06FF]', text))
|
| 39 |
+
|
| 40 |
+
def save_conversation(history, file_path):
|
| 41 |
+
with open(file_path, "w", encoding="utf-8") as f:
|
| 42 |
+
for user, bot in history:
|
| 43 |
+
f.write(f"User: {user}\nAssistant: {bot}\n\n")
|
| 44 |
+
|
| 45 |
+
def load_conversation(file_path):
|
| 46 |
+
history = []
|
| 47 |
+
with open(file_path, encoding="utf-8") as f:
|
| 48 |
+
content = f.read().strip().split("\n\n")
|
| 49 |
+
for turn in content:
|
| 50 |
+
if turn.strip():
|
| 51 |
+
parts = turn.split("\n")
|
| 52 |
+
if len(parts) == 2:
|
| 53 |
+
user = parts[0].replace("User: ", "")
|
| 54 |
+
bot = parts[1].replace("Assistant: ", "")
|
| 55 |
+
history.append((user, bot))
|
| 56 |
+
return history
|
| 57 |
+
|
| 58 |
+
def list_conversations():
|
| 59 |
+
files = os.listdir("conversations")
|
| 60 |
+
files.sort(reverse=True)
|
| 61 |
+
return [f for f in files if f.endswith(".txt")]
|
| 62 |
+
|
| 63 |
+
# ================== تابع اصلی چت ==================
|
| 64 |
+
|
| 65 |
+
def chat_with_bot(message, history, selected_file):
|
| 66 |
+
is_farsi = detect_language(message)
|
| 67 |
history = history or []
|
| 68 |
+
|
| 69 |
+
full_prompt = ""
|
| 70 |
+
for user, bot in history:
|
| 71 |
+
full_prompt += f"User: {user}\nAssistant: {bot}\n"
|
| 72 |
+
full_prompt += f"User: {message}\nAssistant:"
|
| 73 |
+
|
| 74 |
+
if is_farsi:
|
| 75 |
+
inputs = fa_tokenizer(full_prompt, return_tensors="pt", truncation=True)
|
| 76 |
+
outputs = fa_model.generate(**inputs, max_new_tokens=100, pad_token_id=fa_tokenizer.eos_token_id)
|
| 77 |
+
response = fa_tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 78 |
else:
|
| 79 |
+
inputs = en_tokenizer(full_prompt, return_tensors="pt", truncation=True)
|
| 80 |
+
outputs = en_model.generate(**inputs, max_new_tokens=150, pad_token_id=en_tokenizer.eos_token_id)
|
| 81 |
+
response = en_tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 82 |
|
| 83 |
+
response = response.split("Assistant:")[-1].strip()
|
| 84 |
+
history.append((message, response))
|
| 85 |
+
|
| 86 |
+
timestamp = selected_file if selected_file else datetime.now().strftime("%Y-%m-%d_%H-%M") + ".txt"
|
| 87 |
+
save_conversation(history, os.path.join("conversations", timestamp))
|
| 88 |
+
|
| 89 |
+
# تولید صدا
|
| 90 |
+
tts = gTTS(text=response, lang='fa' if is_farsi else 'en')
|
| 91 |
+
audio_path = tempfile.NamedTemporaryFile(suffix=".mp3", delete=False).name
|
| 92 |
+
tts.save(audio_path)
|
| 93 |
+
|
| 94 |
+
return history, history, audio_path, list_conversations(), timestamp
|
| 95 |
+
|
| 96 |
+
# ================== پردازش فایلها ==================
|
| 97 |
+
|
| 98 |
+
def handle_pdf(file):
|
| 99 |
+
reader = PdfReader(file.name)
|
| 100 |
+
text = "\n".join(page.extract_text() for page in reader.pages if page.extract_text())
|
| 101 |
+
summary = text[:1000] + ("..." if len(text) > 1000 else "")
|
| 102 |
+
return summary
|
| 103 |
+
|
| 104 |
+
def handle_image(file):
|
| 105 |
+
import pytesseract
|
| 106 |
+
image = Image.open(file.name)
|
| 107 |
+
return pytesseract.image_to_string(image, lang='fas+eng')
|
| 108 |
+
|
| 109 |
+
def handle_audio(file):
|
| 110 |
+
recognizer = sr.Recognizer()
|
| 111 |
+
with sr.AudioFile(file.name) as source:
|
| 112 |
+
audio_data = recognizer.record(source)
|
| 113 |
+
try:
|
| 114 |
+
text = recognizer.recognize_google(audio_data, language="fa-IR")
|
| 115 |
+
except:
|
| 116 |
+
text = "صدا قابل شناسایی نبود."
|
| 117 |
+
return text
|
| 118 |
+
|
| 119 |
+
def generate_image(prompt):
|
| 120 |
+
image = image_pipe(prompt).images[0]
|
| 121 |
+
return image
|
| 122 |
+
|
| 123 |
+
# ================== رابط گرافیکی ==================
|
| 124 |
|
| 125 |
with gr.Blocks() as demo:
|
| 126 |
+
gr.Markdown("# AIChat-FaEnPro | دستیار هوشمند فارسی-انگلیسی")
|
| 127 |
+
|
| 128 |
+
with gr.Row():
|
| 129 |
+
with gr.Column():
|
| 130 |
+
chatbot = gr.Chatbot()
|
| 131 |
+
msg = gr.Textbox(label="پیام شما")
|
| 132 |
+
audio_in = gr.Audio(source="microphone", type="filepath", label="ورودی صوتی")
|
| 133 |
+
submit = gr.Button("ارسال")
|
| 134 |
+
clear = gr.Button("پاک کردن گفتگو")
|
| 135 |
+
archive_dropdown = gr.Dropdown(label="گفتگوهای ذخیرهشده", choices=list_conversations(), interactive=True)
|
| 136 |
+
|
| 137 |
+
with gr.Column():
|
| 138 |
+
file_input = gr.File(label="آپلود فایل (PDF/تصویر/صدا)")
|
| 139 |
+
file_output = gr.Textbox(label="نتیجه پردازش فایل")
|
| 140 |
+
image_prompt = gr.Textbox(label="دستور تولید تصویر")
|
| 141 |
+
image_out = gr.Image(label="تصویر تولید شده")
|
| 142 |
+
audio_out = gr.Audio(label="صدای پاسخ")
|
| 143 |
+
|
| 144 |
+
state = gr.State([])
|
| 145 |
+
file_state = gr.State("")
|
| 146 |
+
|
| 147 |
+
submit.click(chat_with_bot, [msg, state, file_state], [chatbot, state, audio_out, archive_dropdown, file_state])
|
| 148 |
+
clear.click(lambda: ([], [], None, list_conversations(), ""), None, [chatbot, state, audio_out, archive_dropdown, file_state])
|
| 149 |
+
archive_dropdown.change(lambda f: (load_conversation(os.path.join("conversations", f)), f), [archive_dropdown], [state, file_state])
|
| 150 |
+
audio_in.change(lambda f, h, s: chat_with_bot(handle_audio(f), h, s), [audio_in, state, file_state], [chatbot, state, audio_out, archive_dropdown, file_state])
|
| 151 |
+
file_input.change(lambda f: handle_pdf(f) if f.name.endswith(".pdf") else handle_image(f) if f.type.startswith("image") else handle_audio(f), file_input, file_output)
|
| 152 |
+
image_prompt.submit(generate_image, image_prompt, image_out)
|
| 153 |
|
| 154 |
+
gr.Markdown("طراحی شده توسط شما، با قدرت ChatGPT")
|
|
|
|
| 155 |
|
| 156 |
demo.launch()
|