Skip to content

app.py #3950

@ppp26262626263636262626

Description

from flask import Flask, request, jsonify, send_from_directory

from flask_cors import CORS
import google.generativeai as genai

تنظیم کلید API

API_KEY = ""
genai.configure(api_key=API_KEY)

ساخت مدل و شروع چت

model = genai.GenerativeModel('gemini-2.0-flash-lite-001')
chat = model.start_chat()

ایجاد اپلیکیشن Flask

app = Flask(name)
CORS(app)

نمایش فایل HTML در مسیر اصلی

@app.route('/')
def home():
return send_from_directory('.', '23.html')

مسیر API برای دریافت پیام از کاربر

@app.route('/ask', methods=['POST'])
def ask():
data = request.get_json()
prompt = data.get("prompt", "").strip().lower()

# لیست سوالات مرتبط با ساعت کاری
working_time_phrases = [
    "ساعت کاری", "چه ساعتی باز", "ساعت باز بودن", 
    "چه زمانی باز هستید", "کی بازید", "تا چه ساعتی بازید",
    "از چه ساعتی بازید", "چه تایمی هستید", "چه ساعتی بسته می‌شید"
]

# اگر پیام کاربر شامل یکی از عبارت‌های بالا بود
if any(phrase in prompt for phrase in working_time_phrases):
    return jsonify({
        "response": "🕒 ساعت کاری شرکت از ساعت 8 صبح تا 17 عصر می‌باشد."
    })

# در غیر این صورت، ارسال پیام به Gemini
try:
    response = chat.send_message(prompt)
    return jsonify({"response": response.text})
except Exception as e:
    return jsonify({"response": f"❌ خطا در دریافت پاسخ: {str(e)}"})

اجرای اپلیکیشن

if name == 'main':
app.run(debug=True)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions