Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,10 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
"""
|
| 5 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 6 |
-
"""
|
| 7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 8 |
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
def respond(
|
| 11 |
message,
|
|
@@ -18,31 +17,24 @@ def respond(
|
|
| 18 |
messages = [{"role": "system", "content": system_message}]
|
| 19 |
|
| 20 |
for val in history:
|
| 21 |
-
if val[0]:
|
| 22 |
messages.append({"role": "user", "content": val[0]})
|
| 23 |
-
if val[1]:
|
| 24 |
messages.append({"role": "assistant", "content": val[1]})
|
| 25 |
|
| 26 |
messages.append({"role": "user", "content": message})
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
messages,
|
| 32 |
-
max_tokens=max_tokens,
|
| 33 |
-
stream=True,
|
| 34 |
-
temperature=temperature,
|
| 35 |
-
top_p=top_p,
|
| 36 |
-
):
|
| 37 |
-
token = message.choices[0].delta.content
|
| 38 |
-
|
| 39 |
-
response += token
|
| 40 |
-
yield response
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
|
| 44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 45 |
-
"""
|
| 46 |
demo = gr.ChatInterface(
|
| 47 |
respond,
|
| 48 |
additional_inputs=[
|
|
@@ -59,6 +51,6 @@ demo = gr.ChatInterface(
|
|
| 59 |
],
|
| 60 |
)
|
| 61 |
|
| 62 |
-
|
| 63 |
if __name__ == "__main__":
|
| 64 |
demo.launch()
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import google.generativeai as genai
|
| 3 |
+
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
# Load API key from Hugging Face Secrets
|
| 6 |
+
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
|
| 7 |
+
genai.configure(api_key=GEMINI_API_KEY)
|
| 8 |
|
| 9 |
def respond(
|
| 10 |
message,
|
|
|
|
| 17 |
messages = [{"role": "system", "content": system_message}]
|
| 18 |
|
| 19 |
for val in history:
|
| 20 |
+
if val[0]: # User message
|
| 21 |
messages.append({"role": "user", "content": val[0]})
|
| 22 |
+
if val[1]: # Assistant response
|
| 23 |
messages.append({"role": "assistant", "content": val[1]})
|
| 24 |
|
| 25 |
messages.append({"role": "user", "content": message})
|
| 26 |
|
| 27 |
+
# Call Gemini API
|
| 28 |
+
model = genai.GenerativeModel("gemini-1.5-flash")
|
| 29 |
+
response = model.generate_content(message, stream=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
+
output = ""
|
| 32 |
+
for chunk in response:
|
| 33 |
+
if chunk.text:
|
| 34 |
+
output += chunk.text
|
| 35 |
+
yield output # Stream response in real-time
|
| 36 |
|
| 37 |
+
# Gradio Interface
|
|
|
|
|
|
|
| 38 |
demo = gr.ChatInterface(
|
| 39 |
respond,
|
| 40 |
additional_inputs=[
|
|
|
|
| 51 |
],
|
| 52 |
)
|
| 53 |
|
|
|
|
| 54 |
if __name__ == "__main__":
|
| 55 |
demo.launch()
|
| 56 |
+
|