Spaces:
Runtime error
Runtime error
Download app.py from EhteshamiAI/ehteshami: direct link, hf CLI and curl.
- Browser
- Download file 1.43 kB
-
https://huggingface.co/spaces/EhteshamiAI/ehteshami/resolve/main/app.py
- Command line
-
hf download hf://spaces/EhteshamiAI/ehteshami/app.py
-
curl -L -o app.py https://huggingface.co/spaces/EhteshamiAI/ehteshami/resolve/main/app.py
1.43 kB
| import gradio as gr | |
| from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline | |
| # مدل متنباز قدرتمند | |
| model_id = "mistralai/Mistral-7B-Instruct-v0.2" | |
| # بارگذاری tokenizer و مدل | |
| tokenizer = AutoTokenizer.from_pretrained(model_id) | |
| model = AutoModelForCausalLM.from_pretrained(model_id, device_map="auto") | |
| # ساخت pipeline تولید متن | |
| generator = pipeline("text-generation", model=model, tokenizer=tokenizer) | |
| # تابع چت | |
| def chat(prompt, history=[]): | |
| input_text = "" | |
| for h in history: | |
| input_text += f"User: {h[0]}\nAI: {h[1]}\n" | |
| input_text += f"User: {prompt}\nAI:" | |
| output = generator( | |
| input_text, | |
| max_new_tokens=256, | |
| do_sample=True, | |
| temperature=0.7 | |
| ) | |
| response = output[0]["generated_text"].split("AI:")[-1].strip() | |
| history.append((prompt, response)) | |
| return response, history | |
| # رابط کاربری Gradio | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# 🤖 Ehteshami AI") | |
| chatbot = gr.Chatbot() | |
| msg = gr.Textbox(placeholder="پیام خود را وارد کنید...") | |
| clear = gr.Button("Clear") | |
| def user_message(user_message, chat_history): | |
| response, chat_history = chat(user_message, chat_history) | |
| return "", chat_history | |
| msg.submit(user_message, [msg, chatbot], [msg, chatbot]) | |
| clear.click(lambda: None, None, chatbot, queue=False) | |
| demo.launch() | |