Spaces:
Sleeping
Sleeping
| import os | |
| import gradio as gr | |
| from huggingface_hub import InferenceClient | |
| # --- Configuration --- | |
| # Connect directly via Hugging Face's Native Client | |
| hf_token = os.environ.get("HF_TOKEN") | |
| if not hf_token: | |
| raise ValueError("HF_TOKEN missing. Please add your Hugging Face Access Token to the Space Secrets.") | |
| # Initialize the Zephyr model | |
| client = InferenceClient( | |
| model="Qwen/Qwen2.5-7B-Instruct", | |
| token=hf_token | |
| ) | |
| def chat_function(message, history): | |
| """ | |
| This function takes the user's message and the chat history, | |
| formats it for the Hugging Face API, and returns the response. | |
| """ | |
| # 1. Format the conversation history into the required structure | |
| messages = [] | |
| for user_msg, ai_msg in history: | |
| messages.append({"role": "user", "content": user_msg}) | |
| messages.append({"role": "assistant", "content": ai_msg}) | |
| # 2. Add the current user's message | |
| messages.append({"role": "user", "content": message}) | |
| # 3. Call the model | |
| try: | |
| response = client.chat_completion(messages, max_tokens=512) | |
| return response.choices[0].message.content | |
| except Exception as e: | |
| return f"An error occurred: {str(e)}" | |
| # --- Gradio UI --- | |
| # Use Gradio's built-in ChatInterface for a clean look | |
| demo = gr.ChatInterface( | |
| fn=chat_function, | |
| title="Direct Chat with Qwen/Qwen2.5-7B-Instruct", | |
| description="This app talks directly to the Qwen/Qwen2.5-7B-Instruct model via Hugging Face Serverless API. No local database is attached." | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() |