Spaces:
Runtime error
Runtime error
Download app.py from MalikSahib1/carouselchatbot: direct link, hf CLI and curl.
- Browser
- Download file 1.14 kB
-
https://huggingface.co/spaces/MalikSahib1/carouselchatbot/resolve/main/app.py
- Command line
-
hf download hf://spaces/MalikSahib1/carouselchatbot/app.py
-
curl -L -o app.py https://huggingface.co/spaces/MalikSahib1/carouselchatbot/resolve/main/app.py
1.14 kB
| import os | |
| import gradio as gr | |
| from langchain_openai import ChatOpenAI | |
| # Initialize OpenAI Chat Model | |
| llm = ChatOpenAI( | |
| model_name="gpt-3.5-turbo", | |
| temperature=0.5, | |
| api_key=os.getenv("OPENAI_API_KEY") | |
| ) | |
| SYSTEM_PROMPT = "You are a helpful assistant and carousel maker expert to answer all user queries." | |
| def get_text_response(user_message, history): | |
| # Start with the system instruction | |
| messages = [("system", SYSTEM_PROMPT)] | |
| # Append previous chat turns (Gradio stores history as list of [user, bot] turns or dicts) | |
| for turn in history: | |
| if isinstance(turn, (list, tuple)): | |
| messages.append(("human", turn[0])) | |
| messages.append(("ai", turn[1])) | |
| elif isinstance(turn, dict): # for newer Gradio versions | |
| messages.append((turn.get("role", "human"), turn.get("content", ""))) | |
| # Append current user prompt | |
| messages.append(("human", user_message)) | |
| # Generate response | |
| response = llm.invoke(messages) | |
| return response.content | |
| # Gradio Interface | |
| demo = gr.ChatInterface(get_text_response) | |
| if __name__ == "__main__": | |
| demo.launch() |