try3 / app.py
Nitzantry1's picture
Update app.py
8c65a84 verified
import os
import gradio as gr
from huggingface_hub import InferenceClient
model_id = 'dicta-il/dictalm-7b-instruct'
# ื™ืฆื™ืจืช API ืฉืœ Hugging Face
api_key = os.getenv('HUGGINGFACE_API_KEY', '')
generator = InferenceClient(model=model_id, token=api_key)
# ืคื•ื ืงืฆื™ื™ืช ื™ืฆื™ืจืช ื”ื˜ืงืกื˜
def chat_with_model(history):
prompt = history[-1]["content"]
try:
response = generator.text_generation(prompt, do_sample=True, min_length=20, max_length=64, top_k=40, top_p=0.92, temperature=0.9)
result = response.get("generated_text", "Error: No generated text found")
except Exception as e:
result = f"Error: {str(e)}"
return history + [{"role": "bot", "content": result}]
# ื™ืฆื™ืจืช ืžืžืฉืง ืžืชืงื“ื ืขื Gradio ื‘ืฆื•ืจืช ืฆ'ื˜-ื‘ื•ื˜ ื‘ืกื’ื ื•ืŸ ืืงื“ืžื™
with gr.Blocks(theme="default") as demo:
gr.HTML("""
<div style="background-color: #f5f5f5; padding: 20px; text-align: center;">
<h1 style="color: #003366; font-family: Arial, sans-serif;">ืฆ'ืื˜ ืขื ืžื•ื“ืœ DictaLM</h1>
<p style="font-family: Arial, sans-serif; color: #333;">ื‘ืจื•ื›ื™ื ื”ื‘ืื™ื ืœืฆ'ืื˜ ื”ืื™ื ื˜ืจืืงื˜ื™ื‘ื™ ืฉืœื ื•, ื”ืžืืคืฉืจ ืœื›ื ืœื”ืชื ืกื•ืช ื‘ืฉื™ื—ื” ืขื ืžื•ื“ืœ AI ืžืชืงื“ื.</p>
</div>
""")
chatbot = gr.Chatbot(label="ืฆ'ืื˜ ืขื ืžื•ื“ืœ DictaLM", type="messages")
with gr.Row():
user_input = gr.Textbox(placeholder="ื”ื›ื ืก ืืช ื”ื”ื•ื“ืขื” ืฉืœืš ื›ืืŸ...", label="", lines=1)
send_button = gr.Button("ืฉืœื—")
def user_chat(history, message):
return history + [{"role": "user", "content": message}], ""
# ืฉืœื™ื—ืช ื”ื”ื•ื“ืขื” ื’ื ื‘ืœื—ื™ืฆื” ืขืœ Enter ื•ื’ื ืขืœ ื™ื“ื™ ืœื—ื™ืฆื” ืขืœ ื›ืคืชื•ืจ "ืฉืœื—"
user_input.submit(fn=user_chat, inputs=[chatbot, user_input], outputs=[chatbot, user_input], queue=False).then(
fn=chat_with_model, inputs=chatbot, outputs=chatbot
)
send_button.click(fn=user_chat, inputs=[chatbot, user_input], outputs=[chatbot, user_input], queue=False).then(
fn=chat_with_model, inputs=chatbot, outputs=chatbot
)
demo.launch()