from fastapi import FastAPI, HTTPException from pydantic import BaseModel from huggingface_hub import InferenceClient import os import gradio as gr app = FastAPI() client = InferenceClient(model="HuggingFaceH4/zephyr-7b-beta", token=os.getenv("huggingface_token")) class Message(BaseModel): input: str history: list @app.get("/") async def home(): return {"message": "Welcome to the chatbot API!"} @app.post("/chat") async def chat(message: Message): try: input_message = message.input # Call the chatbot function response = chatbot(input_message) return {"response": response} except Exception as e: raise HTTPException(status_code=500, detail=str(e)) def chatbot(input, history): try: # Call your chatbot function here message = [{"role": "user", "content": input}] history = [{"role": "system", "content": "You are a helpful assistant."}] messages = history + message output = client.chat_completion( messages=messages, max_tokens=4000, temperature=0.7 ) return output.choices[0].message.content except Exception as e: raise Exception(str(e)) # Define the Gradio chat interface def gradio_chat(input): response = chatbot(input, []) return response # Define Gradio inputs and outputs inputs = [gr.Textbox(lines=5, label="Input")] output = gr.Textbox(label="Response") # Create Gradio interface gr.Interface(chatbot, inputs=inputs, outputs=output, title="Chatbot",fill_height=True).launch()