Spaces:
Sleeping
Sleeping
Update app.py
Browse files1234567891011122 unmodified lines1516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364import gradio as grfrom huggingface_hub import InferenceClient"""For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference"""client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")def respond(message,history: list[tuple[str, str]],2 unmodified linestemperature,top_p,):messages = [{"role": "system", "content": system_message}]for val in history:if val[0]:messages.append({"role": "user", "content": val[0]})if val[1]:messages.append({"role": "assistant", "content": val[1]})messages.append({"role": "user", "content": message})response = ""for message in client.chat_completion(messages,max_tokens=max_tokens,stream=True,temperature=temperature,top_p=top_p,):token = message.choices[0].delta.contentresponse += tokenyield response"""For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface"""demo = gr.ChatInterface(respond,additional_inputs=[gr.Textbox(value="You are a friendly Chatbot.", label="System message"),gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),gr.Slider(minimum=0.1,maximum=1.0,value=0.95,step=0.05,label="Top-p (nucleus sampling)",),],)if __name__ == "__main__":demo.launch()123456789101112131415161718192021222324252627282 unmodified lines31323334353637383940414243444546474849505152535455565758596061626364656667import osimport google.generativeai as genaiimport gradio as gr# 1. Google Generative AI API Anahtarını Ayarlamagenai.configure(api_key=os.environ["AIzaSyB9UfmRdDxk0yMtFuOt9XmOGP5apUiTHx4"])# 2. Model Yapılandırması ve Oturum Başlatmageneration_config = {"temperature": 0.7, # Yanıtların çeşitliğini belirler."top_p": 0.95, # En olası yanıtların %95'ini seçer."top_k": 40, # Belirli kelimeler arasından seçim yapmayı sınırlar."max_output_tokens": 512, # Yanıtın maksimum token sayısı."response_mime_type": "text/plain",}model = genai.GenerativeModel(model_name="gemini-1.5-flash", # Gemini modelini kullanıyoruz.generation_config=generation_config,)# Sohbet oturumu başlatmachat_session = model.start_chat(history=[])# 3. Chatbot Yanıt Fonksiyonudef respond(message,history: list[tuple[str, str]],2 unmodified linestemperature,top_p,):# Sistemin başlangıç mesajını ayarlıyoruzchat_session.history = [{"role": "system", "content": system_message}]# Kullanıcı ve asistan mesajlarını ayarlıyoruzfor user_msg, assistant_msg in history:if user_msg:chat_session.history.append({"role": "user", "content": user_msg})if assistant_msg:chat_session.history.append({"role": "assistant", "content": assistant_msg})# Yeni kullanıcının mesajını ekleyip yanıt alıyoruzchat_session.history.append({"role": "user", "content": message})response = chat_session.send_message(message)# Yanıtı döndürmereturn response.text# 4. Gradio Arayüzünü Tanımlama# Başlangıç sistem mesajını "Merhaba! Ben sanal diş hekiminiz. Nasıl yardımcı olabilirim?" olarak ayarlıyoruz.demo = gr.ChatInterface(fn=respond,additional_inputs=[gr.Textbox(value="Merhaba! Ben sanal diş hekiminiz. Nasıl yardımcı olabilirim? 🦷 😊", label="System message"),gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),],)# 5. Uygulamayı Başlatmaif __name__ == "__main__":demo.launch()
app.py+42-39