File size: 2,251 Bytes
1f50957
 
d479557
1f50957
 
 
 
d479557
 
de1e7cf
d479557
1f50957
d479557
1f50957
d479557
1f50957
 
d479557
1f50957
 
 
 
d479557
1f50957
 
 
 
d479557
1f50957
d479557
1f50957
 
 
 
d479557
de1e7cf
 
 
1f50957
 
 
 
 
 
 
 
 
 
 
 
 
d479557
 
1f50957
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d479557
1f50957
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# -*- coding: utf-8 -*-
"""Chatbot using Python Project.ipynb

Automatically generated by Colab.

Original file is located at
    https://colab.research.google.com/drive/1tNZAGbjtsEUqIylR9_nzpBLhj22zIWJy
"""



API_KEY = "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"

import requests

def chat_with_mistral(user_input):
    url = "https://openrouter.ai/api/v1/chat/completions"

    headers = {
        "Authorization": f"Bearer {API_KEY}",
        "Content-Type": "application/json"
    }

    data = {
        "model": "mistralai/mistral-small-24b-instruct-2501:free",
        "messages": [{"role": "user", "content": user_input}]
    }

    response = requests.post(url, json=data, headers=headers)

    if response.status_code == 200:
        return response.json()["choices"][0]["message"]["content"]
    else:
        return f"Error: {response.status_code} - {response.text}"

#user_input = "what about indian cricket team"
#response = chat_with_mistral(user_input)
#print("Chatbot:", response)

import gradio as gr

def mistral_chatbot(user_input):
    return chat_with_mistral(user_input)

# Created a chatbot interface
chatbot_ui = gr.Interface(
    fn=mistral_chatbot,
    inputs="text",
    outputs="text",
    title="Mistral AI Chatbot",
    description="Chat with an AI-powered assistant using Mistral 7B."
)

# Launching the chatbot
chatbot_ui.launch()

chat_history = []

def chat_with_mistral_context(user_input):
    global chat_history  # Maintain history

    url = "https://api.mistral.ai/v1/chat/completions"
    headers = {
        "Authorization": f"Bearer {API_KEY}",
        "Content-Type": "application/json"
    }

    chat_history.append({"role": "user", "content": user_input})  # Add user message

    data = {
        "model": "mistralai/mistral-small-24b-instruct-2501:free",
        "messages": chat_history
    }

    response = requests.post(url, json=data, headers=headers)

    if response.status_code == 200:
        bot_response = response.json()["choices"][0]["message"]["content"]
        chat_history.append({"role": "assistant", "content": bot_response})  # Add bot response
        return bot_response
    else:
        return f"Error: {response.status_code} - {response.text}"