Update app.py
Browse files
app.py
CHANGED
|
@@ -21,11 +21,7 @@ MODEL_OPTIONS = [
|
|
| 21 |
"liquid/lfm-40b:free"
|
| 22 |
]
|
| 23 |
|
| 24 |
-
|
| 25 |
-
history = []
|
| 26 |
-
|
| 27 |
-
def generate_text(input_text, selected_model, history_state):
|
| 28 |
-
global history
|
| 29 |
response = requests.post(
|
| 30 |
url="https://openrouter.ai/api/v1/chat/completions",
|
| 31 |
headers={
|
|
@@ -46,46 +42,38 @@ def generate_text(input_text, selected_model, history_state):
|
|
| 46 |
|
| 47 |
# Handle errors
|
| 48 |
if response.status_code != 200:
|
| 49 |
-
return f"Error: {response.status_code}, {response.text}"
|
| 50 |
|
| 51 |
# Parse and return the content of the response
|
| 52 |
try:
|
| 53 |
response_json = response.json()
|
| 54 |
-
|
| 55 |
except json.JSONDecodeError:
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
-
|
| 70 |
-
|
| 71 |
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
iface = gr.Interface(
|
| 76 |
-
fn=generate_text,
|
| 77 |
-
inputs=[
|
| 78 |
-
gr.Textbox(lines=2, label="Input Text", placeholder="Enter your query here"),
|
| 79 |
-
gr.Dropdown(choices=MODEL_OPTIONS, label="Select Model", value=MODEL_OPTIONS[0]),
|
| 80 |
-
gr.State()
|
| 81 |
-
],
|
| 82 |
-
outputs=[
|
| 83 |
-
gr.Textbox(label="Response", placeholder="Response will be shown here"),
|
| 84 |
-
gr.Textbox(label="History", placeholder="Interaction history will be shown here", lines=10, interactive=False),
|
| 85 |
-
gr.State()
|
| 86 |
-
],
|
| 87 |
-
title="Chat with OpenRouter Models"
|
| 88 |
-
)
|
| 89 |
|
| 90 |
-
#
|
| 91 |
-
iface.launch(
|
|
|
|
| 21 |
"liquid/lfm-40b:free"
|
| 22 |
]
|
| 23 |
|
| 24 |
+
def generate_text(input_text, selected_model):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
response = requests.post(
|
| 26 |
url="https://openrouter.ai/api/v1/chat/completions",
|
| 27 |
headers={
|
|
|
|
| 42 |
|
| 43 |
# Handle errors
|
| 44 |
if response.status_code != 200:
|
| 45 |
+
return f"Error: {response.status_code}, {response.text}"
|
| 46 |
|
| 47 |
# Parse and return the content of the response
|
| 48 |
try:
|
| 49 |
response_json = response.json()
|
| 50 |
+
return response_json.get("choices", [{}])[0].get("message", {}).get("content", "No content returned.")
|
| 51 |
except json.JSONDecodeError:
|
| 52 |
+
return "Error: Unable to parse response."
|
| 53 |
+
|
| 54 |
+
# Define the Gradio layout using Blocks
|
| 55 |
+
with gr.Blocks() as iface:
|
| 56 |
+
# Inject custom CSS using gr.HTML()
|
| 57 |
+
gr.HTML("""
|
| 58 |
+
<style>
|
| 59 |
+
#output-response, #output-history {
|
| 60 |
+
height: 300px;
|
| 61 |
+
overflow: auto;
|
| 62 |
+
border: 1px solid #ddd;
|
| 63 |
+
padding: 10px;
|
| 64 |
+
}
|
| 65 |
+
</style>
|
| 66 |
+
""")
|
| 67 |
+
|
| 68 |
+
input_text = gr.Textbox(lines=2, label="Input Text", placeholder="Enter your query here")
|
| 69 |
+
selected_model = gr.Dropdown(choices=MODEL_OPTIONS, label="Select Model", value=MODEL_OPTIONS[0])
|
| 70 |
|
| 71 |
+
output_response = gr.Textbox(label="Response", placeholder="Response will be shown here")
|
| 72 |
+
output_history = gr.Textbox(label="History", placeholder="Interaction history will be shown here", lines=10, interactive=False)
|
| 73 |
|
| 74 |
+
# Trigger the function when the user inputs text
|
| 75 |
+
generate_button = gr.Button("Generate")
|
| 76 |
+
generate_button.click(generate_text, inputs=[input_text, selected_model], outputs=[output_response, output_history])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
+
# Launch the interface
|
| 79 |
+
iface.launch()
|