Spaces:
Running
Running
import json | |
from typing import Optional, List, Any | |
import requests | |
import gradio as gr | |
def chat_with_mistral( | |
message: str, | |
history: List[Any], | |
api_key: str, | |
temperature: float = 0.5 | |
) -> str: | |
"""Send chat message to Mistral API and return response.""" | |
if not api_key: | |
return "Please provide an API key." | |
try: | |
response = requests.post( | |
"https://codestral.mistral.ai/v1/chat/completions", | |
headers={ | |
"Authorization": f"Bearer {api_key}", | |
"Content-Type": "application/json" | |
}, | |
json={ | |
"model": "codestral-latest", | |
"messages": [{"role": "user", "content": message}], | |
"temperature": temperature | |
}, | |
timeout=10 | |
) | |
response.raise_for_status() | |
return response.json()["choices"][0]["message"]["content"] | |
except Exception as e: | |
return f"Error: {str(e)}" | |
demo = gr.ChatInterface( | |
fn=chat_with_mistral, | |
title="Codestral Chat", | |
description="Your conversations are private. No data is saved or transmitted. More info at [Codestral](https://docs.mistral.ai/capabilities/code_generation/#codestral)", | |
additional_inputs=[ | |
gr.Textbox(label="API Key", type="password", placeholder="Enter your API key"), | |
gr.Slider(minimum=0, maximum=1, value=0.5, label="Temperature") | |
], | |
type="messages" | |
) | |
if __name__ == "__main__": | |
demo.launch() | |