Spaces:
Sleeping
Sleeping
Download app.py from indefiniteuz/ai-chatbot: direct link, hf CLI and curl.
- Browser
- Download file 3.57 kB
-
https://huggingface.co/spaces/indefiniteuz/ai-chatbot/resolve/main/app.py
- Command line
-
hf download hf://spaces/indefiniteuz/ai-chatbot/app.py
-
curl -L -o app.py https://huggingface.co/spaces/indefiniteuz/ai-chatbot/resolve/main/app.py
3.57 kB
| import gradio as gr | |
| import spaces | |
| from huggingface_hub import InferenceClient | |
| import os | |
| # Xavfsizlik uchun token kod ichida emas, muhit o'zgaruvchisidan (Secrets) olinadi | |
| HF_TOKEN = os.environ.get("HF_TOKEN") | |
| # Qwen-72B - Bu eng aqlli va zo'r o'zbekchada gapiradigan modellardan biri | |
| client_chat = InferenceClient("Qwen/Qwen2.5-72B-Instruct", token=HF_TOKEN) | |
| # Whisper - Ovozni matnga aylantirish uchun (Katta va aniq versiya) | |
| client_audio = InferenceClient("openai/whisper-large-v3-turbo", token=HF_TOKEN) | |
| # Vision - Rasmni tushunish uchun (BLIP modeli rasmni tasvirlab beradi) | |
| client_image = InferenceClient("Salesforce/blip-image-captioning-large", token=HF_TOKEN) | |
| def process_file(file_path): | |
| ext = file_path.lower().split('.')[-1] | |
| if ext in ['wav', 'mp3', 'flac', 'ogg', 'm4a']: | |
| try: | |
| res = client_audio.automatic_speech_recognition(file_path) | |
| return f"[Ovozli xabar tarjimasi]: {res.text}" | |
| except Exception as e: | |
| return f"[Ovozni aniqlashda xatolik yuz berdi]: {e}" | |
| elif ext in ['jpg', 'jpeg', 'png', 'webp']: | |
| try: | |
| res = client_image.image_to_text(file_path) | |
| text = res[0].generated_text if isinstance(res, list) else res.generated_text | |
| return f"[Rasmdagi holat tasviri]: {text}" | |
| except Exception as e: | |
| return f"[Rasmni aniqlashda xatolik yuz berdi]: {e}" | |
| else: | |
| return "[Noma'lum fayl formati]" | |
| def chat_logic(message, history): | |
| text_input = message.get("text", "") | |
| files = message.get("files", []) | |
| context = "" | |
| if files: | |
| for f in files: | |
| file_context = process_file(f) | |
| context += file_context + "\n" | |
| final_prompt = text_input | |
| if context: | |
| final_prompt = f"Quyida foydalanuvchi yuborgan fayllar mazmuni keltirilgan:\n{context}\n\nFoydalanuvchi xabari: {text_input}" | |
| messages = [{"role": "system", "content": "Sen o'zbek tilida aniq va chiroyli javob beradigan aqlli AI yordamchisan. Agar foydalanuvchi rasm yoki ovoz yuborgan bo'lsa, senga 'Fayllar mazmuni' orqali uning nima ekanligi beriladi, shunga asoslanib unga yordam ber. Iloji boricha do'stona bo'l."}] | |
| # History ni to'g'irlash | |
| for h in history: | |
| user_msg = h[0] | |
| bot_msg = h[1] | |
| # Gradio da history da fayllar tuple ko'rinishida keladi, ularni to'g'rilash | |
| if isinstance(user_msg, tuple): | |
| user_msg = "[Fayl yuborilgan]" | |
| if isinstance(bot_msg, tuple): | |
| bot_msg = "[Fayl qabul qilingan]" | |
| if user_msg: | |
| messages.append({"role": "user", "content": str(user_msg)}) | |
| if bot_msg: | |
| messages.append({"role": "assistant", "content": str(bot_msg)}) | |
| messages.append({"role": "user", "content": final_prompt}) | |
| try: | |
| response = "" | |
| for chunk in client_chat.chat_completion( | |
| messages, | |
| max_tokens=1024, | |
| stream=True, | |
| ): | |
| token = chunk.choices[0].delta.content | |
| if token: | |
| response += token | |
| yield response | |
| except Exception as e: | |
| yield f"Kechirasiz, xatolik yuz berdi: {str(e)}" | |
| # Gradio Interfeysi | |
| demo = gr.ChatInterface( | |
| fn=chat_logic, | |
| multimodal=True, | |
| title="Multimodal Sun'iy Intellekt 🤖", | |
| description="Ushbu sun'iy intellekt matn, rasm va ovozni tushunadi! Matn yozing, ovozli xabar (mikrofon orqali) yoki rasm yuklang." | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() | |