| """ |
| Minimal Gradio interface for a simple AI assistant without smolagents |
| |
| This is a standalone version that uses only Hugging Face Inference API directly. |
| It creates a simple Gradio interface for text generation. |
| """ |
|
|
| import os |
| import sys |
| import json |
| import requests |
| import gradio as gr |
|
|
| |
| IS_HF_SPACES = os.environ.get("SPACE_ID") is not None |
|
|
| class MinimalAIAssistant: |
| """ |
| Minimal AI Assistant using Hugging Face Inference API directly |
| """ |
| def __init__(self, api_key=None, model_id="mistralai/Mixtral-8x7B-Instruct-v0.1"): |
| """ |
| Initialize the minimal AI assistant |
| |
| Args: |
| api_key: Hugging Face API key |
| model_id: Model ID to use |
| """ |
| self.api_key = api_key or os.environ.get("HF_API_KEY", "") |
| self.model_id = model_id |
| self.api_url = f"https://api-inference.huggingface.co/models/{model_id}" |
| self.headers = {"Authorization": f"Bearer {self.api_key}"} |
| |
| |
| self.system_prompt = """ |
| You are an advanced AI assistant designed to help with various tasks. |
| You can answer questions, provide information, and assist with problem-solving. |
| Always be helpful, accurate, and concise in your responses. |
| """ |
|
|
| def query(self, prompt): |
| """ |
| Query the model with a prompt |
| |
| Args: |
| prompt: User prompt |
| |
| Returns: |
| Model response |
| """ |
| try: |
| |
| formatted_prompt = f"{self.system_prompt}\n\nUser: {prompt}\n\nAssistant:" |
| |
| |
| payload = { |
| "inputs": formatted_prompt, |
| "parameters": { |
| "max_new_tokens": 1024, |
| "temperature": 0.7, |
| "top_p": 0.95, |
| "do_sample": True |
| } |
| } |
| |
| |
| response = requests.post(self.api_url, headers=self.headers, json=payload) |
| |
| |
| if response.status_code != 200: |
| return f"Error: API returned status code {response.status_code}. {response.text}" |
| |
| |
| result = response.json() |
| |
| |
| if isinstance(result, list) and len(result) > 0: |
| generated_text = result[0].get("generated_text", "") |
| |
| if generated_text.startswith(formatted_prompt): |
| generated_text = generated_text[len(formatted_prompt):].strip() |
| return generated_text |
| else: |
| return "Error: Unexpected response format from API" |
| |
| except Exception as e: |
| return f"Error querying model: {str(e)}" |
|
|
|
|
| def create_gradio_interface(): |
| """ |
| Create a Gradio interface for the minimal AI assistant |
| |
| Returns: |
| Gradio interface |
| """ |
| |
| assistant = MinimalAIAssistant() |
| |
| def process_query(query, api_key=""): |
| """ |
| Process a user query |
| |
| Args: |
| query: User query |
| api_key: Hugging Face API key (optional) |
| |
| Returns: |
| Assistant's response |
| """ |
| |
| if api_key: |
| assistant.api_key = api_key |
| assistant.headers = {"Authorization": f"Bearer {api_key}"} |
| |
| |
| if not assistant.api_key: |
| return "Error: No API key provided. Please enter your Hugging Face API key." |
| |
| |
| return assistant.query(query) |
| |
| |
| with gr.Blocks(title="Minimal AI Assistant") as interface: |
| gr.Markdown("# Minimal AI Assistant") |
| gr.Markdown(""" |
| This is a minimal AI assistant using the Hugging Face Inference API. |
| Enter your query below and the assistant will respond. |
| """) |
| |
| api_key_input = gr.Textbox( |
| label="Hugging Face API Key", |
| placeholder="Enter your Hugging Face API key here...", |
| type="password" |
| ) |
| |
| query_input = gr.Textbox( |
| label="Your Query", |
| placeholder="Enter your query here...", |
| lines=3 |
| ) |
| |
| submit_button = gr.Button("Submit") |
| |
| response_output = gr.Textbox( |
| label="Assistant Response", |
| lines=15 |
| ) |
| |
| |
| gr.Markdown("### Sample Queries") |
| sample_queries = [ |
| "What is the capital of France?", |
| "Explain the concept of machine learning in simple terms.", |
| "Write a short poem about artificial intelligence." |
| ] |
| |
| for query in sample_queries: |
| sample_button = gr.Button(f"Try: {query}") |
| sample_button.click( |
| fn=lambda q=query: q, |
| outputs=query_input |
| ) |
| |
| |
| submit_button.click( |
| fn=process_query, |
| inputs=[query_input, api_key_input], |
| outputs=response_output |
| ) |
| |
| |
| gr.Examples( |
| examples=sample_queries, |
| inputs=query_input |
| ) |
| |
| return interface |
|
|
|
|
| |
| interface = create_gradio_interface() |
|
|
| |
| if __name__ == "__main__": |
| interface.launch() |
|
|