Spaces:
Running
Running
Upload folder using huggingface_hub
Browse files
app.py
CHANGED
|
@@ -1,5 +1,7 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
|
|
|
|
|
|
| 3 |
from huggingface_hub import InferenceClient
|
| 4 |
from duckduckgo_search import DDGS
|
| 5 |
import json
|
|
@@ -7,16 +9,89 @@ import json
|
|
| 7 |
token = os.environ.get("HF_TOKEN")
|
| 8 |
client = InferenceClient("Qwen/Qwen2.5-Coder-32B-Instruct", token=token)
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
SYSTEM_PROMPT = """Tu es Cypher Coder, un agent de programmation IA ultra-intelligent fonctionnant dans un terminal (CLI).
|
| 11 |
Tu as été conçu et développé par DJAKOUA KWANKAM, un brillant étudiant en informatique à l'Institut Universitaire de Douala (IUD).
|
| 12 |
-
Tu
|
| 13 |
|
| 14 |
Tu as accès à des outils locaux (comme lire des fichiers, écrire/modifier des fichiers, exécuter des commandes dans le terminal) qui s'exécutent sur la machine locale de l'utilisateur. Ces outils te sont fournis via le protocole CLI de Cypher Coder.
|
| 15 |
Pour les informations en temps réel ou la documentation externe, tu peux aussi utiliser la recherche web.
|
| 16 |
Sois toujours concis, professionnel et direct dans tes explications de code.
|
| 17 |
"""
|
| 18 |
|
| 19 |
-
|
| 20 |
{
|
| 21 |
"type": "function",
|
| 22 |
"function": {
|
|
@@ -63,7 +138,7 @@ def respond(message, history):
|
|
| 63 |
response = client.chat_completion(
|
| 64 |
messages,
|
| 65 |
max_tokens=2048,
|
| 66 |
-
tools=
|
| 67 |
stream=False
|
| 68 |
)
|
| 69 |
first_response = response.choices[0].message
|
|
@@ -98,7 +173,6 @@ def respond(message, history):
|
|
| 98 |
except Exception as e:
|
| 99 |
yield f"Erreur lors de la génération: {str(e)}"
|
| 100 |
|
| 101 |
-
# Interface Web Gradio avec documentation et présentation esthétique
|
| 102 |
theme = gr.themes.Soft(
|
| 103 |
primary_hue="indigo",
|
| 104 |
secondary_hue="cyan",
|
|
@@ -108,7 +182,6 @@ theme = gr.themes.Soft(
|
|
| 108 |
css = """
|
| 109 |
footer {visibility: hidden}
|
| 110 |
.title-container { text-align: center; margin-bottom: 20px; }
|
| 111 |
-
.pixel-art { font-family: monospace; font-size: 8px; line-height: 8px; text-align: center; color: #4F46E5; }
|
| 112 |
"""
|
| 113 |
|
| 114 |
with gr.Blocks(theme=theme, css=css) as demo:
|
|
@@ -165,5 +238,5 @@ with gr.Blocks(theme=theme, css=css) as demo:
|
|
| 165 |
- 🌐 **Recherche Web** : Rechercher sur internet en temps réel pour obtenir la documentation de dernière minute.
|
| 166 |
""")
|
| 167 |
|
| 168 |
-
|
| 169 |
-
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
+
from fastapi import FastAPI, Request
|
| 4 |
+
from fastapi.responses import JSONResponse, StreamingResponse
|
| 5 |
from huggingface_hub import InferenceClient
|
| 6 |
from duckduckgo_search import DDGS
|
| 7 |
import json
|
|
|
|
| 9 |
token = os.environ.get("HF_TOKEN")
|
| 10 |
client = InferenceClient("Qwen/Qwen2.5-Coder-32B-Instruct", token=token)
|
| 11 |
|
| 12 |
+
app = FastAPI()
|
| 13 |
+
|
| 14 |
+
# -----------------------------------------------------
|
| 15 |
+
# Custom API Endpoint for the Cypher Coder CLI Client
|
| 16 |
+
# -----------------------------------------------------
|
| 17 |
+
@app.post("/api/chat")
|
| 18 |
+
async def chat(request: Request):
|
| 19 |
+
try:
|
| 20 |
+
body = await request.json()
|
| 21 |
+
messages = body.get("messages", [])
|
| 22 |
+
tools = body.get("tools", None)
|
| 23 |
+
stream = body.get("stream", False)
|
| 24 |
+
|
| 25 |
+
if not stream:
|
| 26 |
+
response = client.chat_completion(
|
| 27 |
+
messages=messages,
|
| 28 |
+
tools=tools,
|
| 29 |
+
max_tokens=2048,
|
| 30 |
+
stream=False
|
| 31 |
+
)
|
| 32 |
+
choice = response.choices[0]
|
| 33 |
+
message_data = {
|
| 34 |
+
"role": choice.message.role,
|
| 35 |
+
"content": choice.message.content
|
| 36 |
+
}
|
| 37 |
+
if choice.message.tool_calls:
|
| 38 |
+
message_data["tool_calls"] = [
|
| 39 |
+
{
|
| 40 |
+
"id": tc.id,
|
| 41 |
+
"type": tc.type,
|
| 42 |
+
"function": {
|
| 43 |
+
"name": tc.function.name,
|
| 44 |
+
"arguments": tc.function.arguments
|
| 45 |
+
}
|
| 46 |
+
} for tc in choice.message.tool_calls
|
| 47 |
+
]
|
| 48 |
+
return JSONResponse(content={"message": message_data})
|
| 49 |
+
else:
|
| 50 |
+
def event_generator():
|
| 51 |
+
try:
|
| 52 |
+
stream_res = client.chat_completion(
|
| 53 |
+
messages=messages,
|
| 54 |
+
tools=tools,
|
| 55 |
+
max_tokens=2048,
|
| 56 |
+
stream=True
|
| 57 |
+
)
|
| 58 |
+
for chunk in stream_res:
|
| 59 |
+
delta = chunk.choices[0].delta
|
| 60 |
+
content = delta.content or ""
|
| 61 |
+
tool_calls = []
|
| 62 |
+
if delta.tool_calls:
|
| 63 |
+
for tc in delta.tool_calls:
|
| 64 |
+
tool_calls.append({
|
| 65 |
+
"index": tc.index,
|
| 66 |
+
"id": tc.id,
|
| 67 |
+
"type": tc.type,
|
| 68 |
+
"function": {
|
| 69 |
+
"name": tc.function.name,
|
| 70 |
+
"arguments": tc.function.arguments
|
| 71 |
+
}
|
| 72 |
+
})
|
| 73 |
+
|
| 74 |
+
yield f"data: {json.dumps({'content': content, 'tool_calls': tool_calls})}\n\n"
|
| 75 |
+
except Exception as e:
|
| 76 |
+
yield f"data: {json.dumps({'error': str(e)})}\n\n"
|
| 77 |
+
return StreamingResponse(event_generator(), media_type="text/event-stream")
|
| 78 |
+
|
| 79 |
+
except Exception as e:
|
| 80 |
+
return JSONResponse(content={"error": str(e)}, status_code=500)
|
| 81 |
+
|
| 82 |
+
# -----------------------------------------------------
|
| 83 |
+
# Gradio Web Interface (Documentation & Chat)
|
| 84 |
+
# -----------------------------------------------------
|
| 85 |
SYSTEM_PROMPT = """Tu es Cypher Coder, un agent de programmation IA ultra-intelligent fonctionnant dans un terminal (CLI).
|
| 86 |
Tu as été conçu et développé par DJAKOUA KWANKAM, un brillant étudiant en informatique à l'Institut Universitaire de Douala (IUD).
|
| 87 |
+
Tu devez toujours te présenter comme tel.
|
| 88 |
|
| 89 |
Tu as accès à des outils locaux (comme lire des fichiers, écrire/modifier des fichiers, exécuter des commandes dans le terminal) qui s'exécutent sur la machine locale de l'utilisateur. Ces outils te sont fournis via le protocole CLI de Cypher Coder.
|
| 90 |
Pour les informations en temps réel ou la documentation externe, tu peux aussi utiliser la recherche web.
|
| 91 |
Sois toujours concis, professionnel et direct dans tes explications de code.
|
| 92 |
"""
|
| 93 |
|
| 94 |
+
web_tools = [
|
| 95 |
{
|
| 96 |
"type": "function",
|
| 97 |
"function": {
|
|
|
|
| 138 |
response = client.chat_completion(
|
| 139 |
messages,
|
| 140 |
max_tokens=2048,
|
| 141 |
+
tools=web_tools,
|
| 142 |
stream=False
|
| 143 |
)
|
| 144 |
first_response = response.choices[0].message
|
|
|
|
| 173 |
except Exception as e:
|
| 174 |
yield f"Erreur lors de la génération: {str(e)}"
|
| 175 |
|
|
|
|
| 176 |
theme = gr.themes.Soft(
|
| 177 |
primary_hue="indigo",
|
| 178 |
secondary_hue="cyan",
|
|
|
|
| 182 |
css = """
|
| 183 |
footer {visibility: hidden}
|
| 184 |
.title-container { text-align: center; margin-bottom: 20px; }
|
|
|
|
| 185 |
"""
|
| 186 |
|
| 187 |
with gr.Blocks(theme=theme, css=css) as demo:
|
|
|
|
| 238 |
- 🌐 **Recherche Web** : Rechercher sur internet en temps réel pour obtenir la documentation de dernière minute.
|
| 239 |
""")
|
| 240 |
|
| 241 |
+
# Mount Gradio interface onto FastAPI app
|
| 242 |
+
app = gr.mount_gradio_app(app, demo, path="/")
|