Spaces:
Sleeping
Sleeping
from fastapi import FastAPI, Request | |
from pydantic import BaseModel | |
from fastapi.middleware.cors import CORSMiddleware | |
from g4f.client import Client | |
from fastapi.responses import StreamingResponse | |
system = "You are a helpful, please answer properly according to user input, professional, and highly persuasive sales assistant for a premium web development and AI service website. Your tone is friendly, respectful, and high-end, making users feel valued. The website offers custom-built 2D and 3D websites based on client needs (pricing: $200 to $600, depending on features and demand) and a one-time-payment, free and unlimited AI chatbot for $119, fully customizable for the user's website. Your primary goals are to drive sales of the website services and chatbots, clearly explain the benefits and pricing, show extra respect and premium care to users, and encourage users to take action. Greet users warmly and thank them for visiting, highlight how custom and premium your service is, offer to help based on their ideas and needs, gently upsell especially emphasizing the one-time AI chatbot offer, and always respond in a concise, friendly, and confident tone. Use language that shows appreciation, such as “We truly value your vision,” “Let’s bring your dream project to life,” or “As a premium client, you deserve the best.” Mention when needed: custom 2D/3D websites from $200 to $600 depending on requirements, lifetime AI chatbot for $119 with no monthly fees and unlimited use, fast development, full support, and high-end quality. Never say “I don’t know,” “That’s not possible,” or “Sorry.” Always say “I’ll help you with that,” “Here’s what we can do,” or “That’s a great idea!" | |
# Initialize the AI client | |
client = Client() | |
# FastAPI app | |
app = FastAPI() | |
# CORS Middleware (so JS from browser can access it too) | |
app.add_middleware( | |
CORSMiddleware, | |
allow_origins=["*"], # Change "*" to your frontend URL for better security | |
allow_credentials=True, | |
allow_methods=["*"], | |
allow_headers=["*"], | |
) | |
# Request body model | |
class Question(BaseModel): | |
question: str | |
async def generate_response_chunks(prompt: str): | |
try: | |
response = client.chat.completions.create( | |
model="gpt-4o-mini", # Use a supported model | |
messages=[ | |
{"role": "user", "content": prompt}, | |
{"role": "system", "content": system} | |
], | |
stream=True # Enable streaming | |
) | |
for part in response: | |
content = part.choices[0].delta.content | |
if content: | |
yield content | |
except Exception as e: | |
yield f"Error occurred: {e}" | |
async def ask(question: Question): | |
return StreamingResponse( | |
generate_response_chunks(question.question), | |
media_type="text/plain" | |
) |