Spaces:
Sleeping
Sleeping
Using MistralAI API
Browse files
app.py
CHANGED
@@ -6,18 +6,29 @@ Description: Using a large language model for translation.
|
|
6 |
Author: Didier Guillevic
|
7 |
Date: 2024-09-17
|
8 |
"""
|
9 |
-
import spaces
|
10 |
-
import torch
|
11 |
-
import gradio as gr
|
12 |
-
import transformers
|
13 |
-
from transformers import TextIteratorStreamer
|
14 |
-
from threading import Thread
|
15 |
-
|
16 |
import logging
|
17 |
logger = logging.getLogger(__name__)
|
18 |
logging.basicConfig(level=logging.INFO)
|
19 |
|
20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
#
|
23 |
# Default instruction: translate given text
|
@@ -30,9 +41,8 @@ translation_instruction = (
|
|
30 |
)
|
31 |
|
32 |
#
|
33 |
-
# Generate a response
|
34 |
#
|
35 |
-
@spaces.GPU
|
36 |
def generate_chat_response_streaming(
|
37 |
input_text,
|
38 |
chat_history=None,
|
@@ -41,6 +51,40 @@ def generate_chat_response_streaming(
|
|
41 |
temperature=0.0
|
42 |
):
|
43 |
"""Given some input from the user (and a chat history), generate a response"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
|
45 |
# Conversation up to now
|
46 |
conversation = []
|
@@ -136,7 +180,7 @@ with gr.Blocks() as demo:
|
|
136 |
textbox=textbox,
|
137 |
#clear_btn=None, # Unfortunately, clear_btn also reset the additional inputs. Hence disabling for now.
|
138 |
examples=examples,
|
139 |
-
cache_examples=
|
140 |
retry_btn="Retry",
|
141 |
undo_btn="Undo",
|
142 |
clear_btn="Clear",
|
@@ -152,4 +196,4 @@ with gr.Blocks() as demo:
|
|
152 |
""")
|
153 |
|
154 |
if __name__ == "__main__":
|
155 |
-
demo.launch()
|
|
|
6 |
Author: Didier Guillevic
|
7 |
Date: 2024-09-17
|
8 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
import logging
|
10 |
logger = logging.getLogger(__name__)
|
11 |
logging.basicConfig(level=logging.INFO)
|
12 |
|
13 |
+
import gradio as gr
|
14 |
+
|
15 |
+
#import spaces
|
16 |
+
#import torch
|
17 |
+
#import transformers
|
18 |
+
#from transformers import TextIteratorStreamer
|
19 |
+
#from threading import Thread
|
20 |
+
#from model_llm import tokenizer, model
|
21 |
+
|
22 |
+
import os
|
23 |
+
from mistralai import Mistral
|
24 |
+
|
25 |
+
#
|
26 |
+
# Mistral AI client
|
27 |
+
#
|
28 |
+
api_key = os.environ["MISTRAL_API_KEY"]
|
29 |
+
client = Mistral(api_key=api_key)
|
30 |
+
model_id = "mistral-large-latest" # 128k context window
|
31 |
+
|
32 |
|
33 |
#
|
34 |
# Default instruction: translate given text
|
|
|
41 |
)
|
42 |
|
43 |
#
|
44 |
+
# Generate a response using MistralAI API
|
45 |
#
|
|
|
46 |
def generate_chat_response_streaming(
|
47 |
input_text,
|
48 |
chat_history=None,
|
|
|
51 |
temperature=0.0
|
52 |
):
|
53 |
"""Given some input from the user (and a chat history), generate a response"""
|
54 |
+
|
55 |
+
# messages up to now
|
56 |
+
messages = []
|
57 |
+
if not chat_history:
|
58 |
+
messages.append({'role': 'user', 'content': instruction_message + ' ' + input_text})
|
59 |
+
else:
|
60 |
+
for input, response in chat_history:
|
61 |
+
messages.append({"role": "user", "content": input})
|
62 |
+
messages.append({"role": "assistant", "content": response})
|
63 |
+
messages.append({'role': 'user', 'content': input_text})
|
64 |
+
logger.info(messages)
|
65 |
+
|
66 |
+
# generate response
|
67 |
+
|
68 |
+
# Yield the model response as the tokens are being generated
|
69 |
+
stream_reponse = client.chat.stream(model=model_id, messages=messages)
|
70 |
+
model_response = ""
|
71 |
+
for chunk in stream_reponse:
|
72 |
+
model_response += chunk.data.choices[0].delta.content
|
73 |
+
yield model_response
|
74 |
+
|
75 |
+
|
76 |
+
#
|
77 |
+
# Generate a response given some user input and optional chat history
|
78 |
+
#
|
79 |
+
#@spaces.GPU
|
80 |
+
def generate_chat_response_streaming__(
|
81 |
+
input_text,
|
82 |
+
chat_history=None,
|
83 |
+
instruction_message=translation_instruction,
|
84 |
+
max_new_tokens=1_024,
|
85 |
+
temperature=0.0
|
86 |
+
):
|
87 |
+
"""Given some input from the user (and a chat history), generate a response"""
|
88 |
|
89 |
# Conversation up to now
|
90 |
conversation = []
|
|
|
180 |
textbox=textbox,
|
181 |
#clear_btn=None, # Unfortunately, clear_btn also reset the additional inputs. Hence disabling for now.
|
182 |
examples=examples,
|
183 |
+
cache_examples=False,
|
184 |
retry_btn="Retry",
|
185 |
undo_btn="Undo",
|
186 |
clear_btn="Clear",
|
|
|
196 |
""")
|
197 |
|
198 |
if __name__ == "__main__":
|
199 |
+
demo.launch(show_api=False)
|