Spaces:
Running
Running
Pedro O. Acosta
commited on
Commit
·
6dfd948
1
Parent(s):
ffe9dca
Initial commit
Browse files
app.py
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
from typing import Optional, List, Any
|
3 |
+
import requests
|
4 |
+
import gradio as gr
|
5 |
+
|
6 |
+
|
7 |
+
def chat_with_mistral(
|
8 |
+
message: str,
|
9 |
+
history: List[Any],
|
10 |
+
api_key: str,
|
11 |
+
temperature: float = 0.5
|
12 |
+
) -> str:
|
13 |
+
"""Send chat message to Mistral API and return response."""
|
14 |
+
if not api_key:
|
15 |
+
return "Please provide an API key."
|
16 |
+
|
17 |
+
try:
|
18 |
+
response = requests.post(
|
19 |
+
"https://codestral.mistral.ai/v1/chat/completions",
|
20 |
+
headers={
|
21 |
+
"Authorization": f"Bearer {api_key}",
|
22 |
+
"Content-Type": "application/json"
|
23 |
+
},
|
24 |
+
json={
|
25 |
+
"model": "codestral-latest",
|
26 |
+
"messages": [{"role": "user", "content": message}],
|
27 |
+
"temperature": temperature
|
28 |
+
},
|
29 |
+
timeout=10
|
30 |
+
)
|
31 |
+
response.raise_for_status()
|
32 |
+
return response.json()["choices"][0]["message"]["content"]
|
33 |
+
except Exception as e:
|
34 |
+
return f"Error: {str(e)}"
|
35 |
+
|
36 |
+
|
37 |
+
demo = gr.ChatInterface(
|
38 |
+
fn=chat_with_mistral,
|
39 |
+
title="Codestral Chat",
|
40 |
+
description="Your conversations are private. No data is saved or transmitted. More info at [Codestral](https://docs.mistral.ai/capabilities/code_generation/#codestral)",
|
41 |
+
additional_inputs=[
|
42 |
+
gr.Textbox(label="API Key", type="password", placeholder="Enter your API key"),
|
43 |
+
gr.Slider(minimum=0, maximum=1, value=0.5, label="Temperature")
|
44 |
+
],
|
45 |
+
type="messages"
|
46 |
+
)
|
47 |
+
|
48 |
+
if __name__ == "__main__":
|
49 |
+
demo.launch()
|