nikunjcepatel commited on
Commit
8658e51
·
verified ·
1 Parent(s): 3aea094

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -15
app.py CHANGED
@@ -1,29 +1,33 @@
1
  import gradio as gr
2
  import requests
 
3
  import os
4
 
5
  # Retrieve the Open Router API Key from the Space secrets
6
  API_KEY = os.getenv("OpenRounter_API_KEY")
7
 
8
- def chat_with_openrouter(prompt):
9
- url = "https://api.openrouter.ai/v1/chat/completions"
10
- headers = {"Authorization": "Bearer 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"}
11
- data = {
12
- "model": "openai/gpt-4o-mini-2024-07-18",
13
- "messages": [{"role": "user", "content": prompt}],
 
 
 
 
 
14
  "top_p": 1,
15
  "temperature": 1,
 
 
16
  "repetition_penalty": 1,
17
- "transforms": []
18
- }
19
- response = requests.post(url, headers=headers, json=data)
20
  return response.json().get("choices", [{}])[0].get("message", {}).get("content", "No response")
21
 
22
- iface = gr.Interface(
23
- fn=chat_with_openrouter,
24
- inputs=gr.Textbox(lines=2, placeholder="Ask me anything"),
25
- outputs="text",
26
- title="Chat with OpenRouter",
27
- )
28
 
29
  iface.launch()
 
1
  import gradio as gr
2
  import requests
3
+ import json
4
  import os
5
 
6
  # Retrieve the Open Router API Key from the Space secrets
7
  API_KEY = os.getenv("OpenRounter_API_KEY")
8
 
9
+ def chat_with_openrouter(input_text):
10
+ response = requests.post(
11
+ url="https://openrouter.ai/api/v1/chat/completions",
12
+ headers={
13
+ "Authorization": f"Bearer {API_KEY}"
14
+ },
15
+ data=json.dumps({
16
+ "model": "openai/gpt-4o-mini-2024-07-18", # Optional
17
+ "messages": [
18
+ {"role": "user", "content": input_text}
19
+ ]
20
  "top_p": 1,
21
  "temperature": 1,
22
+ "frequency_penalty": 0,
23
+ "presence_penalty": 0,
24
  "repetition_penalty": 1,
25
+ "top_k": 0,
26
+ })
27
+ )
28
  return response.json().get("choices", [{}])[0].get("message", {}).get("content", "No response")
29
 
30
+ # Create Gradio interface
31
+ iface = gr.Interface(fn=generate_text, inputs="text", outputs="text")
 
 
 
 
32
 
33
  iface.launch()